﻿using Module_Debug.Hardware;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;
//using JOJO_gamma;

namespace Module_Debug.VDDIC_Gamma
{
    public partial class TC1105_Gamma : Form
    {
        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();
        Gamma_Addr GAMMAs = new Gamma_Addr();

        private ushort[] gamma_r = new ushort[23];
        private ushort[] gamma_g = new ushort[23];
        private ushort[] gamma_b = new ushort[23];

        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke 
        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过
        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色

        private int index = 0;
        private int flag1 = 1; //lum
        private int flag2 = 1; //x
        private int flag3 = 1; //y
        double[] gamma22;    //gamma 2.2
        double[] gamma19;    //gamma 1.9
        double[] gamma25;    //gamma 2.5
        double[] reallum = new double[24];
        double[] reallumMain = new double[24];     //主屏的数据存储
        double[] reallumSub = new double[24];      //副屏的数据存储
        double[] realnumtemp = new double[8];
        private byte[] Send_buf = new byte[1];
        private byte[] Rec_tmp = new byte[1];
        private int TC1105errorcheck = 0;


        //创建类型为Person的对象集合
        List<Setting.Gamma_Property> Diven_Gamma = new List<Setting.Gamma_Property>();
        List<Setting.Offset_Property> Diven_Offset = new List<Setting.Offset_Property>();
        string Path = System.AppDomain.CurrentDomain.BaseDirectory + "TC1105" + ".txt";

        private static bool Gamma_adjust_timeout_flag = false;
        //Gamma绑点定义
        //private int[] Parttern_IC69091 = new int[34] { 255, 247, 239, 231, 223, 215, 207, 199, 191, 183, 175, 159, 143, 127, 111, 95, 79, 63, 55, 47, 39, 31, 23, 19, 17, 15, 13, 11, 9, 7, 5, 3, 1, 0 };

        frmMain frmHelp = null;


        public TC1105_Gamma()
        {
            InitializeComponent();

        }

        private void TC1105_Gamma_Load(object sender, EventArgs e)
        {
            //画图部分变量
            comboBox1.Items.Add("R");
            comboBox1.Items.Add("G");
            comboBox1.Items.Add("B");
            comboBox1.SelectedIndex = comboBox1.Items.IndexOf("R");
            this.radiobtnMain.Checked = true;
        }

        public TC1105_Gamma(frmMain TC1105_Gamma)
        {
            frmHelp = TC1105_Gamma;
            InitializeComponent();
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体            
        }
        //TC1105 监听卸载
        private void TC1105_Gamma_FormClosed(object sender, FormClosedEventArgs e)
        {
            ////frmHelp.comPort.Close()
            frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(TC1105_DataReceived);
            frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
            ////frmHelp.comPort.Open();
            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "TC1105_Gamma_FormClosed！\r\n";
            frmHelp.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            frmHelp.Child_window_hide_already = false;
            frmHelp.IC_comboBox.Enabled = true;
        }

        private void TC1105_Gamma_VisibleChanged(object sender, EventArgs e)
        {
            if (frmHelp.Child_window_hide_already == false) //子窗体已开启标识
            {
                ////frmHelp.comPort.Close()
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(TC1105_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                ////frmHelp.comPort.Open();
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "TC1105_Gamma_FormHide！\r\n";
                frmHelp.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(TC1105_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "TC1105_Gamma_FormOpened！\r\n";
                frmHelp.Child_window_hide_already = false; //子窗体已开启标识
            }
        }
        //串口接收
        private void TC1105_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            if (IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmHelp.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                frmHelp.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    // 0x62: TC1105的Gamma读写、寄存器读写、OTP读写    设置  返回
                    if (buffer[0] == 0x62 && Setting.TempRecord.cmdLen > 0)
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }
                        }
                    }
                    //0x2F De-Mura 基本操作
                    else if (buffer[0] == 0x2F && (Setting.TempRecord.cmdLen > 0))
                    {
                        if ((buffer[2] == 0x6d) && (buffer[3] == 0x7e) && (buffer[4] == 0x8a))   //处理demura 配置文件的
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, Rec_tmp, 0, buffer.Count);//复制一条完整数据到具体的数据缓存   
                            data_1_catched = true;
                            buffer.RemoveRange(0, buffer.Count);  //正确分析一条数据，从缓存中移除数据。  
                        }
                        else if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, buffer.Count);  //正确分析一条数据，从缓存中移除数据。  
                            }
                        }
                    }
                    else
                    {
                        buffer.RemoveRange(0, buffer.Count);
                    }
                }

                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        frmHelp.T1.Enabled = false; //停止超时计数
                        if (Rec_tmp[0] == 0x62)
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01: { if (Rec_tmp[4] == 0x00) textBox1.Text = textBox1.Text + "写寄存器\tOK！\r\n"; } break;//寄存器写功能返回状态
                                case 0xa1: { if (Rec_tmp[4] == 0x00) textBox1.Text = textBox1.Text + "Dcs写寄存器\tOK！\r\n"; } break;//寄存器写功能返回状态
                                case 0x02:
                                    {
                                        int i;
                                        string tmp = "";
                                        for (i = 3; i <= (Rec_tmp[2] + 1); i++)
                                        {
                                            tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]);//16进制  获取读出来的值
                                        }
                                        tmp = String.Format("{0:X2}", Rec_tmp[i]) + "," + tmp;

                                        txt_Reg_Data_WR.Text = tmp;
                                        textBox1.Text = textBox1.Text + "寄存器\t读 OK！\r\n";
                                    }
                                    break;
                                case 0x03:
                                    {
                                        if (Rec_tmp[4] == 0x00) textBox1.Text = textBox1.Text + "OTP烧录\tOK！\r\n";
                                    }
                                    break;
                                case 0x23:
                                    {
                                        for (int i = 0; i < 23; i++)
                                        {
                                            RGBValue[i, 0] = ((Rec_tmp[2 * i + 4] & 0x0f) * 256 + Rec_tmp[2 * i + 5]);
                                            //R[4]=02;R[5]=10, 3'd256=3'h100;
                                        }
                                        for (int i = 0; i < 23; i++)
                                        {
                                            RGBValue[i, 1] = ((Rec_tmp[2 * i + 4 + 48] & 0x0f) * 256 + Rec_tmp[2 * i + 5 + 48]);
                                        }
                                        for (int i = 0; i < 23; i++)
                                        {
                                            RGBValue[i, 2] = ((Rec_tmp[2 * i + 4 + 96] & 0x0f) * 256 + Rec_tmp[2 * i + 5 + 96]);
                                        }

                                        for (int i = 0; i < 23; i++)
                                        {
                                            RGBValueSet(array2value, i, 0, 0);
                                            RGBValueSet(array2value, i, 1, 0);
                                            RGBValueSet(array2value, i, 2, 0);
                                        }

                                        textBox1.Text = textBox1.Text + "Gamma数据\t RGB全读 OK.\r\n";
                                    }
                                    break;
                                case 0x08://Write Gamma
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            textBox1.Text = textBox1.Text + "GAMMA \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            textBox1.Text = textBox1.Text + "GAMMA \t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x09://Read Gamma  Rec_tmp[4]未定义
                                    {
                                        if (Setting.TempRecord.TC1105_GammaCmdType == 0x03)//Red Register
                                        {

                                            {
                                                num_TC1105_R0.Value = Decimal.Parse((Rec_tmp[4] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());//0x03,最大0x03ff，0x0f,最大0x07ff
                                                num_TC1105_R1.Value = Decimal.Parse((Rec_tmp[6] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                num_TC1105_R2.Value = Decimal.Parse((Rec_tmp[8] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                num_TC1105_R3.Value = Decimal.Parse((Rec_tmp[10] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                num_TC1105_R4.Value = Decimal.Parse((Rec_tmp[12] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                num_TC1105_R5.Value = Decimal.Parse((Rec_tmp[14] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                num_TC1105_R6.Value = Decimal.Parse((Rec_tmp[16] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                num_TC1105_R7.Value = Decimal.Parse((Rec_tmp[18] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                num_TC1105_R8.Value = Decimal.Parse((Rec_tmp[20] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                num_TC1105_R9.Value = Decimal.Parse((Rec_tmp[22] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                num_TC1105_R10.Value = Decimal.Parse((Rec_tmp[24] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                num_TC1105_R11.Value = Decimal.Parse((Rec_tmp[26] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());

                                                num_TC1105_R12.Value = Decimal.Parse((Rec_tmp[28] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                num_TC1105_R13.Value = Decimal.Parse((Rec_tmp[30] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                num_TC1105_R14.Value = Decimal.Parse((Rec_tmp[32] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                num_TC1105_R15.Value = Decimal.Parse((Rec_tmp[34] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                num_TC1105_R16.Value = Decimal.Parse((Rec_tmp[36] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                num_TC1105_R17.Value = Decimal.Parse((Rec_tmp[38] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                num_TC1105_R18.Value = Decimal.Parse((Rec_tmp[40] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                num_TC1105_R19.Value = Decimal.Parse((Rec_tmp[42] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                num_TC1105_R20.Value = Decimal.Parse((Rec_tmp[44] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                num_TC1105_R21.Value = Decimal.Parse((Rec_tmp[46] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                num_TC1105_R22.Value = Decimal.Parse((Rec_tmp[48] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                //num_TC1105_R23.Value = Decimal.Parse((Rec_tmp[50] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());


                                            }

                                            textBox1.Text = textBox1.Text + "红 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.TC1105_GammaCmdType == 0x04)//Green Register
                                        {

                                            {
                                                num_TC1105_G0.Value = Decimal.Parse((Rec_tmp[4] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                                num_TC1105_G1.Value = Decimal.Parse((Rec_tmp[6] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                num_TC1105_G2.Value = Decimal.Parse((Rec_tmp[8] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                num_TC1105_G3.Value = Decimal.Parse((Rec_tmp[10] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                num_TC1105_G4.Value = Decimal.Parse((Rec_tmp[12] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                num_TC1105_G5.Value = Decimal.Parse((Rec_tmp[14] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                num_TC1105_G6.Value = Decimal.Parse((Rec_tmp[16] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                num_TC1105_G7.Value = Decimal.Parse((Rec_tmp[18] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                num_TC1105_G8.Value = Decimal.Parse((Rec_tmp[20] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                num_TC1105_G9.Value = Decimal.Parse((Rec_tmp[22] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                num_TC1105_G10.Value = Decimal.Parse((Rec_tmp[24] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                num_TC1105_G11.Value = Decimal.Parse((Rec_tmp[26] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());

                                                num_TC1105_G12.Value = Decimal.Parse((Rec_tmp[28] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                num_TC1105_G13.Value = Decimal.Parse((Rec_tmp[30] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                num_TC1105_G14.Value = Decimal.Parse((Rec_tmp[32] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                num_TC1105_G15.Value = Decimal.Parse((Rec_tmp[34] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                num_TC1105_G16.Value = Decimal.Parse((Rec_tmp[36] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                num_TC1105_G17.Value = Decimal.Parse((Rec_tmp[38] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                num_TC1105_G18.Value = Decimal.Parse((Rec_tmp[40] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                num_TC1105_G19.Value = Decimal.Parse((Rec_tmp[42] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                num_TC1105_G20.Value = Decimal.Parse((Rec_tmp[44] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                num_TC1105_G21.Value = Decimal.Parse((Rec_tmp[46] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                num_TC1105_G22.Value = Decimal.Parse((Rec_tmp[48] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                //num_TC1105_G23.Value = Decimal.Parse((Rec_tmp[50] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());

                                            }
                                            textBox1.Text = textBox1.Text + "绿 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.TC1105_GammaCmdType == 0x05)//Blue Register
                                        {

                                            {
                                                num_TC1105_B0.Value = Decimal.Parse((Rec_tmp[4] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                                num_TC1105_B1.Value = Decimal.Parse((Rec_tmp[6] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                num_TC1105_B2.Value = Decimal.Parse((Rec_tmp[8] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                num_TC1105_B3.Value = Decimal.Parse((Rec_tmp[10] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                num_TC1105_B4.Value = Decimal.Parse((Rec_tmp[12] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                num_TC1105_B5.Value = Decimal.Parse((Rec_tmp[14] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                num_TC1105_B6.Value = Decimal.Parse((Rec_tmp[16] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                num_TC1105_B7.Value = Decimal.Parse((Rec_tmp[18] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                num_TC1105_B8.Value = Decimal.Parse((Rec_tmp[20] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                num_TC1105_B9.Value = Decimal.Parse((Rec_tmp[22] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                num_TC1105_B10.Value = Decimal.Parse((Rec_tmp[24] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                num_TC1105_B11.Value = Decimal.Parse((Rec_tmp[26] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());

                                                num_TC1105_B12.Value = Decimal.Parse((Rec_tmp[28] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                num_TC1105_B13.Value = Decimal.Parse((Rec_tmp[30] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                num_TC1105_B14.Value = Decimal.Parse((Rec_tmp[32] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                num_TC1105_B15.Value = Decimal.Parse((Rec_tmp[34] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                num_TC1105_B16.Value = Decimal.Parse((Rec_tmp[36] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                num_TC1105_B17.Value = Decimal.Parse((Rec_tmp[38] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                num_TC1105_B18.Value = Decimal.Parse((Rec_tmp[40] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                num_TC1105_B19.Value = Decimal.Parse((Rec_tmp[42] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                num_TC1105_B20.Value = Decimal.Parse((Rec_tmp[44] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                num_TC1105_B21.Value = Decimal.Parse((Rec_tmp[46] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                num_TC1105_B22.Value = Decimal.Parse((Rec_tmp[48] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                //num_TC1105_B23.Value = Decimal.Parse((Rec_tmp[50] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());


                                            }
                                            textBox1.Text = textBox1.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x07:
                                    {
                                        textBox1.Text = textBox1.Text + "请回读指定标志寄存器确定是否OTP成功！！\r\n";
                                    }
                                    break;

                                default: break;
                            }
                        }
                        else if (Rec_tmp[0] == 0x2F)  //Demura
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01://Flash ID
                                    {
                                        textBox1.Text = textBox1.Text + "Flash ID: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "," + Rec_tmp[6].ToString("x") + "\r\n";
                                    }
                                    break;
                                case 0x02://Flash Setting&De-Mura RAM Enable
                                    {
                                        if (Rec_tmp[3] == 0x03)
                                        {
                                            textBox1.Text = textBox1.Text + "read second EC,8F high value not 05，real value：" + Rec_tmp[4] + "\r\n";
                                        }
                                        else
                                        {
                                            textBox1.Text = textBox1.Text + "De-Mura Check OK.\r\n";
                                        }

                                    }
                                    break;
                                case 0x03://Data Load   
                                    {

                                        textBox1.Text = textBox1.Text + "De-Mura Data Loading  Success ! \r\n";
                                        textBox1.Text = textBox1.Text + "烧录flash中,请稍等40s  \r\n";
                                    }
                                    break;
                                case 0x04://De-Mura RAM Disable
                                    {
                                        if (Rec_tmp[3] == 0x01)
                                        {
                                            textBox1.Text = textBox1.Text + "read first EC,8F value not 04，real value：" + Rec_tmp[4] + "\r\n";
                                        }
                                        else if (Rec_tmp[3] == 0x02)
                                        {
                                            textBox1.Text = textBox1.Text + "read EC,ED vlauenot 10，real value：" + Rec_tmp[4] + "\r\n";
                                        }
                                        else
                                        {
                                            textBox1.Text = textBox1.Text + "Check De-Mura data OK" + "\r\n";
                                        }


                                    }
                                    break;
                                case 0x05://
                                    {
                                        textBox1.Text = textBox1.Text + " Flash Erase Success !\r\n";
                                        MessageBox.Show("擦除完成，请执行下一步。");


                                    }
                                    break;
                                case 0x06://Flash Writing
                                    {
                                        if (Rec_tmp[4] == 0x88)
                                        {
                                            textBox1.Text = textBox1.Text + "De-Mura Data Writing Successful. \r\n";
                                            MessageBox.Show("写入完成，请进行下一步。");
                                        }
                                        else
                                        {
                                            textBox1.Text = textBox1.Text + "Please Re-Write Data. \r\n";
                                            MessageBox.Show("写入失败，请重试。");
                                        }
                                    }
                                    break;
                                case 0x07://load   parameter
                                    {
                                        txt_Reg_Data_WR.Text = "fe,ff";
                                        TC1105_Others_Click(btn_Reg_W_Dcs, e);
                                        textBox1.Text = textBox1.Text + "Load De-Mura Parameter Sucessful ! " + "\r\n";
                                    }
                                    break;
                                case 0x08://CRC Check
                                    {
                                        txt_Reg_Data_WR.Text = "29";
                                        TC1105_Others_Click(btn_Reg_W_Dcs, e);


                                        {
                                            textBox1.Text = textBox1.Text + "mipi ok ! \r\n";
                                        }

                                    }
                                    break;
                                case 0x09://De-Mura Function Off
                                    {
                                        textBox1.Text = textBox1.Text + "De-Mura Function On. \r\n";
                                    }
                                    break;
                                case 0x0A://De-Mura Function On
                                    {
                                        textBox1.Text = textBox1.Text + "De-Mura Function Off. \r\n";
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }

                        textBox1.SelectionStart = this.textBox1.Text.Length;
                        textBox1.ScrollToCaret();//滚动到光标处
                        if (textBox1.Lines.Length > 50) textBox1.Text = null;
                    }));
                }
            }
            finally
            {
                Listening = false;
            }
        }
        //Gamma保存
        private void TC1105_SaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    str = str + "R00," + num_TC1105_R0.Text + "," + "G00," + num_TC1105_G0.Text + "," + "B00," + num_TC1105_B0.Text + "\r\n";
                    str = str + "R01," + num_TC1105_R1.Text + "," + "G01," + num_TC1105_G1.Text + "," + "B01," + num_TC1105_B1.Text + "\r\n";
                    str = str + "R03," + num_TC1105_R2.Text + "," + "G03," + num_TC1105_G2.Text + "," + "B03," + num_TC1105_B2.Text + "\r\n";
                    str = str + "R07," + num_TC1105_R3.Text + "," + "G07," + num_TC1105_G3.Text + "," + "B07," + num_TC1105_B3.Text + "\r\n";
                    str = str + "R11," + num_TC1105_R4.Text + "," + "G11," + num_TC1105_G4.Text + "," + "B11," + num_TC1105_B4.Text + "\r\n";
                    str = str + "R15," + num_TC1105_R5.Text + "," + "G15," + num_TC1105_G5.Text + "," + "B15," + num_TC1105_B5.Text + "\r\n";

                    str = str + "R19," + num_TC1105_R6.Text + "," + "G19," + num_TC1105_G6.Text + "," + "B19," + num_TC1105_B6.Text + "\r\n";
                    str = str + "R23," + num_TC1105_R7.Text + "," + "G23," + num_TC1105_G7.Text + "," + "B23," + num_TC1105_B7.Text + "\r\n";
                    str = str + "R31," + num_TC1105_R8.Text + "," + "G31," + num_TC1105_G8.Text + "," + "B31," + num_TC1105_B8.Text + "\r\n";
                    str = str + "R39," + num_TC1105_R9.Text + "," + "G39," + num_TC1105_G9.Text + "," + "B39," + num_TC1105_B9.Text + "\r\n";
                    str = str + "R47," + num_TC1105_R10.Text + "," + "G47," + num_TC1105_G10.Text + "," + "B47," + num_TC1105_B10.Text + "\r\n";
                    str = str + "R55," + num_TC1105_R11.Text + "," + "G55," + num_TC1105_G11.Text + "," + "B55," + num_TC1105_B11.Text + "\r\n";

                    str = str + "R63," + num_TC1105_R12.Text + "," + "G63," + num_TC1105_G12.Text + "," + "B63," + num_TC1105_B12.Text + "\r\n";
                    str = str + "R79," + num_TC1105_R13.Text + "," + "G79," + num_TC1105_G13.Text + "," + "B79," + num_TC1105_B13.Text + "\r\n";
                    str = str + "R95," + num_TC1105_R14.Text + "," + "G95," + num_TC1105_G14.Text + "," + "B95," + num_TC1105_B14.Text + "\r\n";
                    str = str + "R127," + num_TC1105_R15.Text + "," + "G127," + num_TC1105_G15.Text + "," + "B127," + num_TC1105_B15.Text + "\r\n";
                    str = str + "R159," + num_TC1105_R16.Text + "," + "G159," + num_TC1105_G16.Text + "," + "B159," + num_TC1105_B16.Text + "\r\n";
                    str = str + "R175," + num_TC1105_R17.Text + "," + "G175," + num_TC1105_G17.Text + "," + "B175," + num_TC1105_B17.Text + "\r\n";

                    str = str + "R191," + num_TC1105_R18.Text + "," + "G191," + num_TC1105_G18.Text + "," + "B191," + num_TC1105_B18.Text + "\r\n";
                    str = str + "R207," + num_TC1105_R19.Text + "," + "G207," + num_TC1105_G19.Text + "," + "B207," + num_TC1105_B19.Text + "\r\n";
                    str = str + "R223," + num_TC1105_R20.Text + "," + "G223," + num_TC1105_G20.Text + "," + "B223," + num_TC1105_B20.Text + "\r\n";
                    str = str + "R239," + num_TC1105_R21.Text + "," + "G239," + num_TC1105_G21.Text + "," + "B239," + num_TC1105_B21.Text + "\r\n";
                    str = str + "R255," + num_TC1105_R22.Text + "," + "G255," + num_TC1105_G22.Text + "," + "B255," + num_TC1105_B22.Text + "\r\n";
                    //str = str + "R225," + num_TC1105_R23.Text + "," + "G225," + num_TC1105_G23.Text + "," + "B225," + num_TC1105_B23.Text + "\r\n";


                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        } //
        //Gamma载入
        private void TC1105_LoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                num_TC1105_R0.Text = arrtmp[1].ToString(); num_TC1105_G0.Text = arrtmp[3].ToString(); num_TC1105_B0.Text = arrtmp[5].ToString();
                num_TC1105_R1.Text = arrtmp[7].ToString(); num_TC1105_G1.Text = arrtmp[9].ToString(); num_TC1105_B1.Text = arrtmp[11].ToString();
                num_TC1105_R2.Text = arrtmp[13].ToString(); num_TC1105_G2.Text = arrtmp[15].ToString(); num_TC1105_B2.Text = arrtmp[17].ToString();
                num_TC1105_R3.Text = arrtmp[19].ToString(); num_TC1105_G3.Text = arrtmp[21].ToString(); num_TC1105_B3.Text = arrtmp[23].ToString();
                num_TC1105_R4.Text = arrtmp[25].ToString(); num_TC1105_G4.Text = arrtmp[27].ToString(); num_TC1105_B4.Text = arrtmp[29].ToString();
                num_TC1105_R5.Text = arrtmp[31].ToString(); num_TC1105_G5.Text = arrtmp[33].ToString(); num_TC1105_B5.Text = arrtmp[35].ToString();

                num_TC1105_R6.Text = arrtmp[37].ToString(); num_TC1105_G6.Text = arrtmp[39].ToString(); num_TC1105_B6.Text = arrtmp[41].ToString();
                num_TC1105_R7.Text = arrtmp[43].ToString(); num_TC1105_G7.Text = arrtmp[45].ToString(); num_TC1105_B7.Text = arrtmp[47].ToString();
                num_TC1105_R8.Text = arrtmp[49].ToString(); num_TC1105_G8.Text = arrtmp[51].ToString(); num_TC1105_B8.Text = arrtmp[53].ToString();
                num_TC1105_R9.Text = arrtmp[55].ToString(); num_TC1105_G9.Text = arrtmp[57].ToString(); num_TC1105_B9.Text = arrtmp[59].ToString();
                num_TC1105_R10.Text = arrtmp[61].ToString(); num_TC1105_G10.Text = arrtmp[63].ToString(); num_TC1105_B10.Text = arrtmp[65].ToString();
                num_TC1105_R11.Text = arrtmp[67].ToString(); num_TC1105_G11.Text = arrtmp[69].ToString(); num_TC1105_B11.Text = arrtmp[71].ToString();

                num_TC1105_R12.Text = arrtmp[73].ToString(); num_TC1105_G12.Text = arrtmp[75].ToString(); num_TC1105_B12.Text = arrtmp[77].ToString();
                num_TC1105_R13.Text = arrtmp[79].ToString(); num_TC1105_G13.Text = arrtmp[81].ToString(); num_TC1105_B13.Text = arrtmp[83].ToString();
                num_TC1105_R14.Text = arrtmp[85].ToString(); num_TC1105_G14.Text = arrtmp[87].ToString(); num_TC1105_B14.Text = arrtmp[89].ToString();
                num_TC1105_R15.Text = arrtmp[91].ToString(); num_TC1105_G15.Text = arrtmp[93].ToString(); num_TC1105_B15.Text = arrtmp[95].ToString();
                num_TC1105_R16.Text = arrtmp[97].ToString(); num_TC1105_G16.Text = arrtmp[99].ToString(); num_TC1105_B16.Text = arrtmp[101].ToString();
                num_TC1105_R17.Text = arrtmp[103].ToString(); num_TC1105_G17.Text = arrtmp[105].ToString(); num_TC1105_B17.Text = arrtmp[107].ToString();

                num_TC1105_R18.Text = arrtmp[109].ToString(); num_TC1105_G18.Text = arrtmp[111].ToString(); num_TC1105_B18.Text = arrtmp[113].ToString();
                num_TC1105_R19.Text = arrtmp[115].ToString(); num_TC1105_G19.Text = arrtmp[117].ToString(); num_TC1105_B19.Text = arrtmp[119].ToString();
                num_TC1105_R20.Text = arrtmp[121].ToString(); num_TC1105_G20.Text = arrtmp[123].ToString(); num_TC1105_B20.Text = arrtmp[125].ToString();
                num_TC1105_R21.Text = arrtmp[127].ToString(); num_TC1105_G21.Text = arrtmp[129].ToString(); num_TC1105_B21.Text = arrtmp[131].ToString();
                num_TC1105_R22.Text = arrtmp[133].ToString(); num_TC1105_G22.Text = arrtmp[135].ToString(); num_TC1105_B22.Text = arrtmp[137].ToString();
                //num_TC1105_R23.Text = arrtmp[139].ToString(); num_TC1105_G23.Text = arrtmp[141].ToString(); num_TC1105_B23.Text = arrtmp[143].ToString();

            }
        }
        //Gamma2.2目标值计算
        private void btn_TC1105_Calculate_Click(object sender, EventArgs e)
        {
            double Lr_d, Lg_d, Lb_d;

            string[] arr = TC1105_Gamma_SetBox.Lines;
            {
                try
                {
                    Diven_Gamma.Clear();
                    Diven_Offset.Clear();
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string line = arr[i];
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToInt32(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            //将Offset对象放入集合
                            Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                            Diven_Offset.Add(Offset);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("格式输入有误");
                }
            }
            //----------------------------------------------------------------------------
            try
            {
                txt_TC1105_loff.Text = Diven_Offset[0].LUM_OFF.ToString();
                txt_TC1105_xoff.Text = Diven_Offset[0].X_OFF.ToString();
                txt_TC1105_yoff.Text = Diven_Offset[0].Y_OFF.ToString();

                int Total_points = Diven_Gamma[0].Points_num;
                D_lums = new double[Total_points, 3];
                D_lum = new double[Total_points];
                txt_TC1105_gamma.Text = Diven_Gamma[0].Curve.ToString();
                txt_TC1105_lum.Text = Diven_Gamma[0].Lum.ToString();
                txt_TC1105_x.Text = Diven_Gamma[0].X.ToString();
                txt_TC1105_y.Text = Diven_Gamma[0].Y.ToString();
                Points_LV.Text = "";
                for (int p = 0; p < Total_points; p++)
                {
                    D_lum[p] = (Math.Pow((Diven_Offset[p].NO / 255.0), Diven_Gamma[0].Curve) * Diven_Gamma[0].Lum); //每绑点目标亮度
                    Points_LV.Text = Points_LV.Text + Diven_Offset[p].NO + ":  " + Math.Round(D_lum[p], 6) + "\r\n";
                }

                CaSDKObj.YUV_to_RGB(Diven_Gamma[0].Y, Diven_Gamma[0].X, out Lr_d, out Lb_d, out Lg_d);//R/B/G分亮度转换

                for (int i = 0; i < Total_points; i++)
                {
                    D_lums[i, 0] = D_lum[i] * Lr_d; //R/B/G分亮度计算
                    D_lums[i, 1] = D_lum[i] * Lg_d;
                    D_lums[i, 2] = D_lum[i] * Lb_d;
                }
            }
            catch
            {
                MessageBox.Show("格式输入有误");
            }
        }
        //自动调节启动开关
        private void btn_TC1105_AutoGammas_Click(object sender, EventArgs e)
        {
            index = 0;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            //gamma_r = GAMMAs.TC1105_Gamma_r;
            //gamma_g = GAMMAs.TC1105_Gamma_g;
            //gamma_b = GAMMAs.TC1105_Gamma_b;


            Application.DoEvents();
            frmHelp.OLED_Show_Click(frmHelp.btnwhite, e);
            frmHelp.numPatternR.Value = 255;
            frmHelp.numPatternG.Value = 255;
            frmHelp.numPatternB.Value = 255;
            frmHelp.OLED_Show_Click(frmHelp.btnPatternW, e);


            TC1105_GammaWR_Click(btn_TC1105_GammaW_RED, e);

            TC1105_GammaWR_Click(btn_TC1105_GammaW_GREEN, e);

            TC1105_GammaWR_Click(btn_TC1105_GammaW_BLUE, e);

            btn_TC1105_Calculate_Click(null, null);

            Thread.Sleep(10);
            T20.Enabled = true;

        }
        //自动调节关闭开关  
        private void btn_TC1105_Stop_Click(object sender, EventArgs e)
        {
            T20.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
        }


        private void T20_Tick(object sender, EventArgs e)
        {
            TC1105_Gamma_Tuning(null, null);
        }

        private void 保存gamma绑点配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (TC1105_Gamma_SetBox.Text == string.Empty)
            {
                MessageBox.Show("内容为空!");
            }
            else
            {
                //创建保存对话框对象
                SaveFileDialog save = new SaveFileDialog();
                save.Filter = "Code files (*.txt)|*.txt"; //过滤文件类型
                save.FilterIndex = 1;
                save.RestoreDirectory = true;
                if (save.ShowDialog() == DialogResult.OK)
                {
                    string path = save.FileName;
                    save.DefaultExt = ".txt";
                    StreamWriter sw = new StreamWriter(path);
                    sw.WriteLine(TC1105_Gamma_SetBox.Text);
                    sw.Close();
                }
            }
        }

        private void 载入gamma绑点配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            of.Title = "载入TC1105 gamma绑点";

            if (of.ShowDialog() == DialogResult.OK)
            {
                TC1105_Gamma_SetBox.Clear();
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string str_generate = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(str_generate))
                {
                    TC1105_Gamma_SetBox.AppendText(str_generate + "\r\n");
                    str_generate = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
            }
        }

        private void TC1105_GammaWR_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[53];
            Setting.TempRecord.cmdLen = 53;//
            Send_buf[0] = 0x62; //TC1105
            Send_buf[2] = 48;//0x30  CRC校验-2，-1了
            //------------------------------------选择哪个Page-----------------------------------------
            int R_page = comboBoxR.SelectedIndex + 1;    //0代表set2---6代表set8
            //int G_page = comboBoxG.SelectedIndex+1;
            //int B_page = comboBoxB.SelectedIndex+1;
            int G_page = R_page;
            int B_page = R_page;
            //------------------------------------------------------------------------------------------
            switch (btnTmp.Name)
            {
                case "btn_TC1105_GammaW_RED":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] |= 0x10; //RED
                        Send_buf[4] = (byte)(R_page & 0xFF);           //用于发送哪个set下的
                        {
                            Send_buf[5] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R0.Value.ToString()) >> 8);
                            Send_buf[6] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R0.Value.ToString())) & 0xff);
                            Send_buf[7] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R1.Value.ToString()) >> 8);
                            Send_buf[8] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R1.Value.ToString())) & 0xff);
                            Send_buf[9] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R2.Value.ToString()) >> 8);
                            Send_buf[10] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R2.Value.ToString())) & 0xff);
                            Send_buf[11] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R3.Value.ToString()) >> 8);
                            Send_buf[12] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R3.Value.ToString())) & 0xff);
                            Send_buf[13] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R4.Value.ToString()) >> 8);
                            Send_buf[14] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R4.Value.ToString())) & 0xff);
                            Send_buf[15] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R5.Value.ToString()) >> 8);
                            Send_buf[16] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R5.Value.ToString())) & 0xff);
                            Send_buf[17] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R6.Value.ToString()) >> 8);
                            Send_buf[18] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R6.Value.ToString())) & 0xff);
                            Send_buf[19] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R7.Value.ToString()) >> 8);
                            Send_buf[20] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R7.Value.ToString())) & 0xff);
                            Send_buf[21] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R8.Value.ToString()) >> 8);
                            Send_buf[22] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R8.Value.ToString())) & 0xff);
                            Send_buf[23] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R9.Value.ToString()) >> 8);
                            Send_buf[24] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R9.Value.ToString())) & 0xff);
                            Send_buf[25] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R10.Value.ToString()) >> 8);
                            Send_buf[26] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R10.Value.ToString())) & 0xff);
                            Send_buf[27] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R11.Value.ToString()) >> 8);
                            Send_buf[28] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R11.Value.ToString())) & 0xff);
                            Send_buf[29] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R12.Value.ToString()) >> 8);
                            Send_buf[30] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R12.Value.ToString())) & 0xff);
                            Send_buf[31] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R13.Value.ToString()) >> 8);
                            Send_buf[32] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R13.Value.ToString())) & 0xff);
                            Send_buf[33] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R14.Value.ToString()) >> 8);
                            Send_buf[34] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R14.Value.ToString())) & 0xff);
                            Send_buf[35] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R15.Value.ToString()) >> 8);
                            Send_buf[36] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R15.Value.ToString())) & 0xff);
                            Send_buf[37] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R16.Value.ToString()) >> 8);
                            Send_buf[38] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R16.Value.ToString())) & 0xff);
                            Send_buf[39] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R17.Value.ToString()) >> 8);
                            Send_buf[40] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R17.Value.ToString())) & 0xff);
                            Send_buf[41] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R18.Value.ToString()) >> 8);
                            Send_buf[42] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R18.Value.ToString())) & 0xff);
                            Send_buf[43] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R19.Value.ToString()) >> 8);
                            Send_buf[44] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R19.Value.ToString())) & 0xff);
                            Send_buf[45] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R20.Value.ToString()) >> 8);
                            Send_buf[46] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R20.Value.ToString())) & 0xff);
                            Send_buf[47] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R21.Value.ToString()) >> 8);
                            Send_buf[48] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R21.Value.ToString())) & 0xff);
                            Send_buf[49] = Convert.ToByte(Convert.ToUInt16(num_TC1105_R22.Value.ToString()) >> 8);
                            Send_buf[50] = Convert.ToByte((Convert.ToUInt16(num_TC1105_R22.Value.ToString())) & 0xff);
                        }
                    }
                    break;
                case "btn_TC1105_GammaW_GREEN":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] |= 0x20; //GREEN    
                        Send_buf[4] = (byte)(G_page & 0xFF);           //用于发送哪个set下的
                        {
                            Send_buf[5] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G0.Value.ToString()) >> 8);
                            Send_buf[6] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G0.Value.ToString())) & 0xff);
                            Send_buf[7] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G1.Value.ToString()) >> 8);
                            Send_buf[8] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G1.Value.ToString())) & 0xff);
                            Send_buf[9] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G2.Value.ToString()) >> 8);
                            Send_buf[10] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G2.Value.ToString())) & 0xff);
                            Send_buf[11] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G3.Value.ToString()) >> 8);
                            Send_buf[12] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G3.Value.ToString())) & 0xff);
                            Send_buf[13] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G4.Value.ToString()) >> 8);
                            Send_buf[14] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G4.Value.ToString())) & 0xff);
                            Send_buf[15] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G5.Value.ToString()) >> 8);
                            Send_buf[16] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G5.Value.ToString())) & 0xff);
                            Send_buf[17] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G6.Value.ToString()) >> 8);
                            Send_buf[18] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G6.Value.ToString())) & 0xff);
                            Send_buf[19] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G7.Value.ToString()) >> 8);
                            Send_buf[20] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G7.Value.ToString())) & 0xff);
                            Send_buf[21] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G8.Value.ToString()) >> 8);
                            Send_buf[22] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G8.Value.ToString())) & 0xff);
                            Send_buf[23] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G9.Value.ToString()) >> 8);
                            Send_buf[24] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G9.Value.ToString())) & 0xff);
                            Send_buf[25] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G10.Value.ToString()) >> 8);
                            Send_buf[26] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G10.Value.ToString())) & 0xff);
                            Send_buf[27] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G11.Value.ToString()) >> 8);
                            Send_buf[28] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G11.Value.ToString())) & 0xff);
                            Send_buf[29] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G12.Value.ToString()) >> 8);
                            Send_buf[30] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G12.Value.ToString())) & 0xff);
                            Send_buf[31] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G13.Value.ToString()) >> 8);
                            Send_buf[32] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G13.Value.ToString())) & 0xff);
                            Send_buf[33] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G14.Value.ToString()) >> 8);
                            Send_buf[34] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G14.Value.ToString())) & 0xff);
                            Send_buf[35] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G15.Value.ToString()) >> 8);
                            Send_buf[36] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G15.Value.ToString())) & 0xff);
                            Send_buf[37] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G16.Value.ToString()) >> 8);
                            Send_buf[38] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G16.Value.ToString())) & 0xff);
                            Send_buf[39] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G17.Value.ToString()) >> 8);
                            Send_buf[40] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G17.Value.ToString())) & 0xff);
                            Send_buf[41] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G18.Value.ToString()) >> 8);
                            Send_buf[42] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G18.Value.ToString())) & 0xff);
                            Send_buf[43] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G19.Value.ToString()) >> 8);
                            Send_buf[44] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G19.Value.ToString())) & 0xff);
                            Send_buf[45] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G20.Value.ToString()) >> 8);
                            Send_buf[46] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G20.Value.ToString())) & 0xff);
                            Send_buf[47] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G21.Value.ToString()) >> 8);
                            Send_buf[48] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G21.Value.ToString())) & 0xff);
                            Send_buf[49] = Convert.ToByte(Convert.ToUInt16(num_TC1105_G22.Value.ToString()) >> 8);
                            Send_buf[50] = Convert.ToByte((Convert.ToUInt16(num_TC1105_G22.Value.ToString())) & 0xff);
                        }
                    }
                    break;
                case "btn_TC1105_GammaW_BLUE":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] |= 0x30; //BLUE
                        Send_buf[4] = (byte)(B_page & 0xFF);           //用于发送哪个set下的
                        {
                            Send_buf[5] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B0.Value.ToString()) >> 8);
                            Send_buf[6] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B0.Value.ToString())) & 0xff);
                            Send_buf[7] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B1.Value.ToString()) >> 8);
                            Send_buf[8] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B1.Value.ToString())) & 0xff);
                            Send_buf[9] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B2.Value.ToString()) >> 8);
                            Send_buf[10] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B2.Value.ToString())) & 0xff);
                            Send_buf[11] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B3.Value.ToString()) >> 8);
                            Send_buf[12] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B3.Value.ToString())) & 0xff);
                            Send_buf[13] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B4.Value.ToString()) >> 8);
                            Send_buf[14] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B4.Value.ToString())) & 0xff);
                            Send_buf[15] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B5.Value.ToString()) >> 8);
                            Send_buf[16] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B5.Value.ToString())) & 0xff);
                            Send_buf[17] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B6.Value.ToString()) >> 8);
                            Send_buf[18] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B6.Value.ToString())) & 0xff);
                            Send_buf[19] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B7.Value.ToString()) >> 8);
                            Send_buf[20] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B7.Value.ToString())) & 0xff);
                            Send_buf[21] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B8.Value.ToString()) >> 8);
                            Send_buf[22] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B8.Value.ToString())) & 0xff);
                            Send_buf[23] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B9.Value.ToString()) >> 8);
                            Send_buf[24] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B9.Value.ToString())) & 0xff);
                            Send_buf[25] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B10.Value.ToString()) >> 8);
                            Send_buf[26] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B10.Value.ToString())) & 0xff);
                            Send_buf[27] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B11.Value.ToString()) >> 8);
                            Send_buf[28] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B11.Value.ToString())) & 0xff);
                            Send_buf[29] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B12.Value.ToString()) >> 8);
                            Send_buf[30] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B12.Value.ToString())) & 0xff);
                            Send_buf[31] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B13.Value.ToString()) >> 8);
                            Send_buf[32] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B13.Value.ToString())) & 0xff);
                            Send_buf[33] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B14.Value.ToString()) >> 8);
                            Send_buf[34] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B14.Value.ToString())) & 0xff);
                            Send_buf[35] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B15.Value.ToString()) >> 8);
                            Send_buf[36] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B15.Value.ToString())) & 0xff);
                            Send_buf[37] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B16.Value.ToString()) >> 8);
                            Send_buf[38] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B16.Value.ToString())) & 0xff);
                            Send_buf[39] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B17.Value.ToString()) >> 8);
                            Send_buf[40] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B17.Value.ToString())) & 0xff);
                            Send_buf[41] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B18.Value.ToString()) >> 8);
                            Send_buf[42] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B18.Value.ToString())) & 0xff);
                            Send_buf[43] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B19.Value.ToString()) >> 8);
                            Send_buf[44] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B19.Value.ToString())) & 0xff);
                            Send_buf[45] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B20.Value.ToString()) >> 8);
                            Send_buf[46] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B20.Value.ToString())) & 0xff);
                            Send_buf[47] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B21.Value.ToString()) >> 8);
                            Send_buf[48] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B21.Value.ToString())) & 0xff);
                            Send_buf[49] = Convert.ToByte(Convert.ToUInt16(num_TC1105_B22.Value.ToString()) >> 8);
                            Send_buf[50] = Convert.ToByte((Convert.ToUInt16(num_TC1105_B22.Value.ToString())) & 0xff);
                        }
                    }
                    break;
                //-----------------------------------------------------------------------
                case "btn_TC1105_GammaR_RED"://0x56,0x09,0x49,0x10
                    {
                        Setting.TempRecord.TC1105_GammaCmdType = 0x03;
                        Send_buf[1] = 0x09;
                        Send_buf[3] |= 0x10;
                        Send_buf[4] = (byte)(R_page & 0xFF);           //用于发送哪个set下的
                    }
                    break;
                case "btn_TC1105_GammaR_GREEN":
                    {
                        Setting.TempRecord.TC1105_GammaCmdType = 0x04;
                        Send_buf[1] = 0x09;
                        Send_buf[3] |= 0x20;
                        Send_buf[4] = (byte)(G_page & 0xFF);           //用于发送哪个set下的
                    }
                    break;
                case "btn_TC1105_GammaR_BLUE":
                    {
                        Setting.TempRecord.TC1105_GammaCmdType = 0x05;
                        Send_buf[1] = 0x09;
                        Send_buf[3] |= 0x30;
                        Send_buf[4] = (byte)(B_page & 0xFF);           //用于发送哪个set下的
                    }
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[Setting.TempRecord.cmdLen - 2], out Send_buf[Setting.TempRecord.cmdLen - 1]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, Setting.TempRecord.cmdLen);   //
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }

        private void TC1105_GammaWR_All_Click(object sender, EventArgs e)
        {
        }
        private int[] Point_TC1105 = new int[23] {//---Gamma绑点数据，这里的数据仅供参考---
            255, 239, 223,  207, 191, 175, 159, 127, 95, 79, 63, 55, 47, 39, 31, 23, 19, 15, 11, 7, 3,1,0 };
        private static int[,] RGBValue = new int[23, 3];
        bool array2value = false;   //记录值写入窗体控件(记录绑点RGB寄存器值)的标志位
        bool value2array = true;    //窗体控件值保存到记录标志位
        private void RGBValueSet(bool inoutflag, int index, byte rgb, byte extraInfo)    //把窗体控件上Gamma绑点寄存器值保存于数组，便于后续处理
        {
            NumericUpDown[] NUMG = new NumericUpDown[] { num_TC1105_G22, num_TC1105_G21, num_TC1105_G20, num_TC1105_G19, num_TC1105_G18, num_TC1105_G17, num_TC1105_G16, num_TC1105_G15, num_TC1105_G14, num_TC1105_G13, num_TC1105_G12, num_TC1105_G11, num_TC1105_G10, num_TC1105_G9, num_TC1105_G8, num_TC1105_G7, num_TC1105_G6, num_TC1105_G5, num_TC1105_G4, num_TC1105_G3, num_TC1105_G2, num_TC1105_G1, num_TC1105_G0 };
            NumericUpDown[] NUMR = new NumericUpDown[] { num_TC1105_R22, num_TC1105_R21, num_TC1105_R20, num_TC1105_R19, num_TC1105_R18, num_TC1105_R17, num_TC1105_R16, num_TC1105_R15, num_TC1105_R14, num_TC1105_R13, num_TC1105_R12, num_TC1105_R11, num_TC1105_R10, num_TC1105_R9, num_TC1105_R8, num_TC1105_R7, num_TC1105_R6, num_TC1105_R5, num_TC1105_R4, num_TC1105_R3, num_TC1105_R2, num_TC1105_R1, num_TC1105_R0 };
            NumericUpDown[] NUMB = new NumericUpDown[] { num_TC1105_B22, num_TC1105_B21, num_TC1105_B20, num_TC1105_B19, num_TC1105_B18, num_TC1105_B17, num_TC1105_B16, num_TC1105_B15, num_TC1105_B14, num_TC1105_B13, num_TC1105_B12, num_TC1105_B11, num_TC1105_B10, num_TC1105_B9, num_TC1105_B8, num_TC1105_B7, num_TC1105_B6, num_TC1105_B5, num_TC1105_B4, num_TC1105_B3, num_TC1105_B2, num_TC1105_B1, num_TC1105_B0 };
            if (inoutflag)
            {
                switch (rgb)
                {
                    case 0:
                        RGBValue[index, 0] = (int)NUMR[index].Value;
                        break;
                    case 1:
                        RGBValue[index, 1] = (int)NUMG[index].Value;
                        break;
                    case 2:
                        RGBValue[index, 2] = (int)NUMB[index].Value;
                        break;
                }
            }
            else
            {
                switch (rgb)
                {
                    case 0:
                        NUMR[index].Value = RGBValue[index, 0];
                        break;
                    case 1:
                        NUMG[index].Value = RGBValue[index, 1];
                        break;
                    case 2:
                        NUMB[index].Value = RGBValue[index, 2];
                        break;
                }
            }
        }
        private void TC1105_Gamma_Tuning(Object myObject, EventArgs e)
        {
            int total_points = Diven_Gamma[0].Points_num;
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            Thread.Sleep(10);
            frmHelp.Msr_CASDK();

            if (Gamma_adjust_timeout_flag == true)//如果是0绑点
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
            }
            else
            {
                if ((frmHelp.CASDK_LUM < (D_lum[index] + D_lum[index] * Diven_Offset[index].LUM_OFF / 100)) && (frmHelp.CASDK_LUM > (D_lum[index] - D_lum[index] * Diven_Offset[index].LUM_OFF / 100)))//G
                { flag1 = 0; }
                else
                { flag1 = 1; }
                if ((frmHelp.CASDK_x < (Diven_Gamma[0].X + Diven_Offset[index].X_OFF)) && (frmHelp.CASDK_x > (Diven_Gamma[0].X - Diven_Offset[index].X_OFF)) && flag1 == 0)//R
                { flag3 = 0; }
                else
                { flag3 = 1; }
                if ((frmHelp.CASDK_y < (Diven_Gamma[0].Y + Diven_Offset[index].Y_OFF)) && (frmHelp.CASDK_y > (Diven_Gamma[0].Y - Diven_Offset[index].Y_OFF)) && flag1 == 0 && flag3 == 0)//B
                { flag2 = 0; }
                else
                { flag2 = 1; }

            }
            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                //--------
                index++;

                if (index == total_points)
                {
                    textBox1.Text = textBox1.Text + "TC1105 Gamma调试完毕!\r\n";
                }
                if (index < total_points)
                {
                    frmHelp.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                    frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                    frmHelp.numPatternB.Value = Diven_Offset[index].NO;

                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数

                    Thread.Sleep(50); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                    TC1105errorcheck = 0; //解灰阶没有切过去的bug

                    //解灰阶没有切过去的bug  ---切了2次
                    frmHelp.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                    frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                    frmHelp.numPatternB.Value = Diven_Offset[index].NO;

                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数

                    Thread.Sleep(50); //1秒=1000毫秒，
                }
            }
            else
            {
                decimal r = 0, g = 0, b = 0;
                CaSDKObj.YUV_to_RGB(frmHelp.CASDK_y, frmHelp.CASDK_x, out Lr_t, out Lb_t, out Lg_t);

                lumr = frmHelp.CASDK_LUM * Lr_t;
                lumg = frmHelp.CASDK_LUM * Lg_t;
                lumb = frmHelp.CASDK_LUM * Lb_t;

                try
                {
                    switch (index)
                    {
                        #region//
                        case 22:   //0
                            {
                                //public void Flexible_Gamma_auto_Adujst(
                                //int lum_adjust_enable, int x_adjust_enable, int y_adjust_enable,
                                //double measure_lum, double measure_x, double measure_y,
                                //double target_lum, double target_x, double target_y,
                                //decimal input_reg_red, decimal input_reg_green, decimal input_reg_blue,
                                //int gary_target, int reg_range,
                                ////output
                                //out decimal output_reg_red, out decimal output_reg_green, out decimal output_reg_blue, out bool timeout_flag)

                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R0.Value, num_TC1105_G0.Value, num_TC1105_B0.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R0.Value = r = 0;
                                num_TC1105_G0.Value = g = 0;
                                num_TC1105_B0.Value = b = 0;
                            }
                            break;
                        case 21:    //1
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R1.Value, num_TC1105_G1.Value, num_TC1105_B1.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R1.Value = r;
                                num_TC1105_G1.Value = g;
                                num_TC1105_B1.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R1.Value, (UInt16)num_TC1105_G1.Value, (UInt16)num_TC1105_B1.Value);
                            }
                            break;

                        case 20:   //3
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R2.Value, num_TC1105_G2.Value, num_TC1105_B2.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R2.Value = r;
                                num_TC1105_G2.Value = g;
                                num_TC1105_B2.Value = b;
                                //     auto_gamma_write_TC1105((UInt16)num_TC1105_R2.Value, (UInt16)num_TC1105_G2.Value, (UInt16)num_TC1105_B2.Value);
                            }
                            break;

                        case 19:   //7
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R3.Value, num_TC1105_G3.Value, num_TC1105_B3.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R3.Value = r;
                                num_TC1105_G3.Value = g;
                                num_TC1105_B3.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R3.Value, (UInt16)num_TC1105_G3.Value, (UInt16)num_TC1105_B3.Value);
                            }
                            break;

                        case 18:  //11
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R4.Value, num_TC1105_G4.Value, num_TC1105_B4.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R4.Value = r;
                                num_TC1105_G4.Value = g;
                                num_TC1105_B4.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R4.Value, (UInt16)num_TC1105_G4.Value, (UInt16)num_TC1105_B4.Value);
                            }
                            break;

                        case 17:   //15
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R5.Value, num_TC1105_G5.Value, num_TC1105_B5.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R5.Value = r;
                                num_TC1105_G5.Value = g;
                                num_TC1105_B5.Value = b;
                                //     auto_gamma_write_TC1105((UInt16)num_TC1105_R5.Value, (UInt16)num_TC1105_G5.Value, (UInt16)num_TC1105_B5.Value);

                            }
                            break;

                        case 16:    //19
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R6.Value, num_TC1105_G6.Value, num_TC1105_B6.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R6.Value = r;
                                num_TC1105_G6.Value = g;
                                num_TC1105_B6.Value = b;
                                //   auto_gamma_write_TC1105((UInt16)num_TC1105_R6.Value, (UInt16)num_TC1105_G6.Value, (UInt16)num_TC1105_B6.Value);

                            }
                            break;

                        case 15:   //23
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R7.Value, num_TC1105_G7.Value, num_TC1105_B7.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R7.Value = r;
                                num_TC1105_G7.Value = g;
                                num_TC1105_B7.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R7.Value, (UInt16)num_TC1105_G7.Value, (UInt16)num_TC1105_B7.Value);

                            }
                            break;

                        case 14:    //31
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R8.Value, num_TC1105_G8.Value, num_TC1105_B8.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R8.Value = r;
                                num_TC1105_G8.Value = g;
                                num_TC1105_B8.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R8.Value, (UInt16)num_TC1105_G8.Value, (UInt16)num_TC1105_B8.Value);

                            }
                            break;

                        case 13:   //39
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R9.Value, num_TC1105_G9.Value, num_TC1105_B9.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R9.Value = r;
                                num_TC1105_G9.Value = g;
                                num_TC1105_B9.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R9.Value, (UInt16)num_TC1105_G9.Value, (UInt16)num_TC1105_B9.Value);

                            }
                            break;

                        case 12:  //47
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R10.Value, num_TC1105_G10.Value, num_TC1105_B10.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R10.Value = r;
                                num_TC1105_G10.Value = g;
                                num_TC1105_B10.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R10.Value, (UInt16)num_TC1105_G10.Value, (UInt16)num_TC1105_B10.Value);

                            }
                            break;

                        case 11:  //55
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R11.Value, num_TC1105_G11.Value, num_TC1105_B11.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R11.Value = r;
                                num_TC1105_G11.Value = g;
                                num_TC1105_B11.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R11.Value, (UInt16)num_TC1105_G11.Value, (UInt16)num_TC1105_B11.Value);
                            }
                            break;
                        case 10:  //63
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R12.Value, num_TC1105_G12.Value, num_TC1105_B12.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R12.Value = r;
                                num_TC1105_G12.Value = g;
                                num_TC1105_B12.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R12.Value, (UInt16)num_TC1105_G12.Value, (UInt16)num_TC1105_B12.Value);
                            }
                            break;
                        case 9:  //79
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R13.Value, num_TC1105_G13.Value, num_TC1105_B13.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R13.Value = r;
                                num_TC1105_G13.Value = g;
                                num_TC1105_B13.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R13.Value, (UInt16)num_TC1105_G13.Value, (UInt16)num_TC1105_B13.Value);
                            }
                            break;
                        case 8:   //95
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R14.Value, num_TC1105_G14.Value, num_TC1105_B14.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R14.Value = r;
                                num_TC1105_G14.Value = g;
                                num_TC1105_B14.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R14.Value, (UInt16)num_TC1105_G14.Value, (UInt16)num_TC1105_B14.Value);
                            }
                            break;
                        case 7:   //127
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R15.Value, num_TC1105_G15.Value, num_TC1105_B15.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R15.Value = r;
                                num_TC1105_G15.Value = g;
                                num_TC1105_B15.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R15.Value, (UInt16)num_TC1105_G15.Value, (UInt16)num_TC1105_B15.Value);
                            }
                            break;
                        case 6:   //159
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R16.Value, num_TC1105_G16.Value, num_TC1105_B16.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R16.Value = r;
                                num_TC1105_G16.Value = g;
                                num_TC1105_B16.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R16.Value, (UInt16)num_TC1105_G16.Value, (UInt16)num_TC1105_B16.Value);
                            }
                            break;
                        case 5:   //175
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R17.Value, num_TC1105_G17.Value, num_TC1105_B17.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R17.Value = r;
                                num_TC1105_G17.Value = g;
                                num_TC1105_B17.Value = b;
                                //   auto_gamma_write_TC1105((UInt16)num_TC1105_R17.Value, (UInt16)num_TC1105_G17.Value, (UInt16)num_TC1105_B17.Value);
                            }
                            break;
                        case 4:   //191
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R18.Value, num_TC1105_G18.Value, num_TC1105_B18.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R18.Value = r;
                                num_TC1105_G18.Value = g;
                                num_TC1105_B18.Value = b;
                                //   auto_gamma_write_TC1105((UInt16)num_TC1105_R18.Value, (UInt16)num_TC1105_G18.Value, (UInt16)num_TC1105_B18.Value);
                            }
                            break;
                        case 3:   //207
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R19.Value, num_TC1105_G19.Value, num_TC1105_B19.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R19.Value = r;
                                num_TC1105_G19.Value = g;
                                num_TC1105_B19.Value = b;
                                //   auto_gamma_write_TC1105((UInt16)num_TC1105_R19.Value, (UInt16)num_TC1105_G19.Value, (UInt16)num_TC1105_B19.Value);
                            }
                            break;
                        case 2:    //223
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R20.Value, num_TC1105_G20.Value, num_TC1105_B20.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R20.Value = r;
                                num_TC1105_G20.Value = g;
                                num_TC1105_B20.Value = b;
                                //   auto_gamma_write_TC1105((UInt16)num_TC1105_R20.Value, (UInt16)num_TC1105_G20.Value, (UInt16)num_TC1105_B20.Value);
                            }
                            break;
                        case 1:    //239
                            {

                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R21.Value, num_TC1105_G21.Value, num_TC1105_B21.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R21.Value = r;
                                num_TC1105_G21.Value = g;
                                num_TC1105_B21.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R21.Value, (UInt16)num_TC1105_G21.Value, (UInt16)num_TC1105_B21.Value);
                            }
                            break;
                        case 0:   //255绑点
                            {

                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], num_TC1105_R22.Value, num_TC1105_G22.Value, num_TC1105_B22.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                num_TC1105_R22.Value = r;
                                num_TC1105_G22.Value = g;
                                num_TC1105_B22.Value = b;
                                //    auto_gamma_write_TC1105((UInt16)num_TC1105_R22.Value, (UInt16)num_TC1105_G22.Value, (UInt16)num_TC1105_B22.Value);
                            }
                            break;
                        default:
                            break;

                            #endregion

                    }
                    #region//
                    if (index != 22)
                    {
                        auto_gamma_write_TC1105((UInt16)r, (UInt16)g, (UInt16)b, index);
                        Thread.Sleep(10);
                        if (index < 21)
                        {
                            if (frmHelp.CASDK_LUM < 0.015)
                            {
                                TC1105errorcheck++;
                                if (TC1105errorcheck > 5)
                                {
                                    frmHelp.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                                    frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                                    frmHelp.numPatternB.Value = Diven_Offset[index].NO;

                                    Application.DoEvents();
                                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                                    Thread.Sleep(50); //1秒=1000毫秒，
                                    TC1105errorcheck = 0;
                                }
                            }
                        }
                        else if (index == 21)
                        {
                            if (frmHelp.CASDK_LUM < 0.0015)
                            {
                                TC1105errorcheck++;
                                if (TC1105errorcheck > 5)
                                {
                                    frmHelp.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                                    frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                                    frmHelp.numPatternB.Value = Diven_Offset[index].NO;

                                    Application.DoEvents();
                                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数

                                    Thread.Sleep(50); //1秒=1000毫秒，
                                }
                            }
                        }
                    }
                    else
                    {
                        auto_gamma_write_TC1105(0, 0, 0, index);
                        Thread.Sleep(15);
                        TC1105_GammaWR_Click(btn_TC1105_GammaW_RED, e);
                        Thread.Sleep(15);
                        TC1105_GammaWR_Click(btn_TC1105_GammaW_GREEN, e);
                        Thread.Sleep(15);
                        TC1105_GammaWR_Click(btn_TC1105_GammaW_BLUE, e);
                        Thread.Sleep(15);
                        T20.Enabled = false;
                    }
                    #endregion
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("调整数值超限");
                }
            }
        }


        public void auto_gamma_write_TC1105(UInt16 datar, UInt16 datag, UInt16 Datab, int index_tep)//一组gamma写入
        {
            //获取当前的
            int R_page1 = comboBoxR.SelectedIndex + 1;    //0代表set2---6代表set8
            //int G_page1 = comboBoxG.SelectedIndex + 1;
            //int B_page1 = comboBoxB.SelectedIndex + 1;
            int G_page1 = R_page1;
            int B_page1 = R_page1;
            Send_buf = new byte[13];
            Setting.TempRecord.cmdLen = 13;
            Send_buf[0] = 0x62;//195
            Send_buf[1] = 0xDD;
            Send_buf[2] = 8;
            Send_buf[3] = (byte)(R_page1 & 0xFF);    //代表哪个set 1代表set2
            Send_buf[4] = (byte)(index_tep & 0xFF);      //发送哪个绑点  0代表255绑点.
            Send_buf[5] = Convert.ToByte(datar >> 8);       //r
            Send_buf[6] = Convert.ToByte(datar & 0xff);
            Send_buf[7] = Convert.ToByte(datag >> 8);      //g
            Send_buf[8] = Convert.ToByte(datag & 0xff);
            Send_buf[9] = Convert.ToByte(Datab >> 8);      //b
            Send_buf[10] = Convert.ToByte(Datab & 0xff);

            CRCData.CalculateCrc16(Send_buf, out Send_buf[11], out Send_buf[12]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 13);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
        }



        public static void Delay(int milliSecond)
        {
            int start = Environment.TickCount;
            while (Math.Abs(Environment.TickCount - start) < milliSecond)//毫秒
            {
                Application.DoEvents();//可执行某无聊的操作
            }
        }

        #region  //--------------------风神算法------------------
        /*
        输出：15，16，17、目标绑点R/G/B寄存器输出值；
              18、超时提醒
        */
        //public decimal Red_Ratio = 0xf;
        //public decimal Green_Ratio = Convert.ToDecimal();
        //public decimal Blue_Ratio = Convert.ToDecimal();
        //--------------系数----------------------------------------------------
        public decimal Last_reg_red = 0;
        public decimal Last_reg_green = 0;
        public decimal Last_reg_blue = 0;
        //--------------多组计算------------------------------------------------
        private ArrayList flexible_gamma_point_list = new ArrayList(); //存储灰阶
        private ArrayList flexible_gamma_reg_red = new ArrayList();    //存储每阶R寄存器
        private ArrayList flexible_gamma_reg_green = new ArrayList();  //存储每阶G寄存器
        private ArrayList flexible_gamma_reg_blue = new ArrayList();   //存储每阶B寄存器
        //--------------单段计算------------------------------------------------
        private static decimal[,] Past_Reg_stack = new decimal[3, 250];  //存储每跳步R/G/B寄存器值
        private static double[,] Past_lum_stack = new double[3, 250];    //存储每跳步R/G/B分亮度值   
        private static double[] BCompare0 = new double[250];
        //--------------单点加速------------------------------------------------
        private static bool Register_jump_flag = false;
        private static bool Register_lum_jump_flag = false;
        //public bool Gamma_adjust_timeout_flag = false;
        public int Gamma_auto_adjust_count = 0;
        public int Gamma_auto_adjust_count_temp = 0;
        //--------------单色加速-------------------------------------------------
        private static int Y_adjust_count = 0;
        private static int X_adjust_count = 0;
        //-----------------------------------------------------------------------
        private static int Steph = 0, Stepm = 0, Stepl = 0;
        //-----------------------------------------------------------------------
        /// <summary>
        /// Gamma绑点寄存器调整
        /// </summary>
        /// <param name="lum_adjust_enable">亮度分量调整使能</param>
        /// <param name="x_adjust_enable">X色坐标分量调整使能</param>
        /// <param name="y_adjust_enable">Y色坐标分量调整使能</param>
        /// <param name="measure_lum">计算实际测量亮度分量</param>
        /// <param name="measure_x">计算实际测量X色坐标分量</param>
        /// <param name="measure_y">计算实际测量Y色坐标分量</param>
        /// <param name="target_lum">目标亮度分量</param>
        /// <param name="target_x">目标X色坐标分量</param>
        /// <param name="target_y">目标Y色坐标分量</param>
        /// <param name="input_reg_red">输入_R_寄存器值</param>
        /// <param name="input_reg_green">输入_G_寄存器值</param>
        /// <param name="input_reg_blue">输入_B_寄存器值</param>
        /// <param name="gary_target">目标灰阶值</param>
        /// <param name="reg_range">寄存器调整范围</param>
        /// <param name="output_reg_red">输出_R_寄存器值</param>
        /// <param name="output_reg_green">输出_G_寄存器值</param>
        /// <param name="output_reg_blue">输出_B_寄存器值</param>
        /// <param name="timeout_flag">调节超时标志位</param>
        public void Flexible_Gamma_auto_Adujst(
        int lum_adjust_enable, int x_adjust_enable, int y_adjust_enable,
        double measure_lum, double measure_x, double measure_y,
        double target_lum, double target_x, double target_y,
        decimal input_reg_red, decimal input_reg_green, decimal input_reg_blue,
        int gary_target, int reg_range,
        //output
        out decimal output_reg_red, out decimal output_reg_green, out decimal output_reg_blue, out bool timeout_flag)
        {
            int step_tmp = 0;
            double max_h, min_h, max_m, min_m;
            int adjust_times, shake_times;
            double target_reference;
            decimal reg_reference;
            int reg_approach = 0;
            timeout_flag = false;
            output_reg_red = input_reg_red;
            output_reg_green = input_reg_green;
            output_reg_blue = input_reg_blue;
            target_reference = target_lum + target_x + target_y;
            //自动单绑点调节次数
            if (reg_range > 0x7ff) //FFF~800
            {
                adjust_times = 120;
            }
            else if (reg_range > 0x3ff) //7ff~400
            {
                adjust_times = 90;
            }
            else
            {
                adjust_times = 70;
            }
            try
            {
                if (Gamma_auto_adjust_count <= adjust_times)
                {
                    if (Gamma_auto_adjust_count == 0)//firstly!clear buffer 
                    {
                        //根据可调range，自动定义步长，随灰阶绑点逐渐从Max减小至Min
                        max_h = 50.13 * Math.Sin(0.0003539 * reg_range + 0.04827);
                        min_h = 30.06 * Math.Sin(0.0003796 * reg_range - 0.04927);
                        max_m = 25.03 * Math.Sin(0.0004312 * reg_range - 0.24);
                        min_m = 13.11 * Math.Sin(0.0002326 * reg_range - 0.08475);
                        Steph = (int)(gary_target / 255 * max_h + min_h);
                        Stepm = (int)(gary_target / 255 * max_m + min_m);
                        Stepl = 1;

                        if (gary_target == 255)//如果是每组的第一个绑点，清空List
                        {
                            flexible_gamma_point_list.Clear();
                            flexible_gamma_reg_red.Clear();
                            flexible_gamma_reg_green.Clear();
                            flexible_gamma_reg_blue.Clear();
                        }

                        Register_jump_flag = false;
                        Register_lum_jump_flag = false;
                        //记录上个绑点调节最佳值
                        Last_reg_red = Past_Reg_stack[0, Gamma_auto_adjust_count_temp];
                        Last_reg_green = Past_Reg_stack[1, Gamma_auto_adjust_count_temp];
                        Last_reg_blue = Past_Reg_stack[2, Gamma_auto_adjust_count_temp];

                        //灰阶 list 有效数据从buffer[0]开始记录
                        //    flexible_gamma_point_list.Add(target_gray);
                        //RGB寄存器 list 在数据清空前记录每阶绑点最佳值,有效数据从buffer[1]开始记录
                        flexible_gamma_reg_red.Add(Last_reg_red);
                        flexible_gamma_reg_green.Add(Last_reg_green);
                        flexible_gamma_reg_blue.Add(Last_reg_blue);

                        Past_Reg_stack = new decimal[3, 250];
                        Past_lum_stack = new double[3, 250];
                        BCompare0 = new double[250];
                        Gamma_auto_adjust_count_temp = 0;//清“0”重新开始统计

                        //加速跳转
                        if ((gary_target != 255) && (Math.Abs(measure_lum - target_lum) > target_lum * 10 / 100) && (((input_reg_red > Last_reg_red) || (input_reg_red < Last_reg_red - 0x70)) || ((input_reg_green > Last_reg_green) || (input_reg_green < Last_reg_green - 0x70)) || ((input_reg_blue > Last_reg_blue) || (input_reg_blue < Last_reg_blue - 0x70)))) //R/G/B启动初始值干预   ,此处判断条件后面改掉!!
                        {
                            output_reg_red = Last_reg_red - Convert.ToDecimal(txt_gamma_adj_step.Text); //减去多少值后续由计算得出，此处暂定减去0xf
                            output_reg_green = Last_reg_green - Convert.ToDecimal(txt_gamma_adj_step.Text);
                            output_reg_blue = Last_reg_blue - Convert.ToDecimal(txt_gamma_adj_step.Text);
                            //output_reg_red = (decimal)(Math.Pow((double)(gary_target) * 190.5 , 0.3498));
                            //output_reg_green = (decimal)(Math.Pow((double)(gary_target) * 173.3, 0.3609));
                            //output_reg_blue = (decimal)(Math.Pow((double)(gary_target) * 207.3, 0.3514));

                            Gamma_auto_adjust_count = Gamma_auto_adjust_count + 1;
                            Gamma_auto_adjust_count_temp = Gamma_auto_adjust_count;//记录最佳值地址指针
                            goto Jump_to_end;
                        }
                        else//debug
                        {
                            textBox1.Text = textBox1.Text + "gary_target" + gary_target.ToString() + "\r\n";
                            textBox1.Text = textBox1.Text + "input_reg_red" + input_reg_red.ToString() + "\r\n";
                            textBox1.Text = textBox1.Text + "input_reg_green" + input_reg_green.ToString() + "\r\n";
                            textBox1.Text = textBox1.Text + "input_reg_blue" + input_reg_blue.ToString() + "\r\n";
                        }
                    }
                    Gamma_auto_adjust_count = Gamma_auto_adjust_count + 1;
                    Gamma_auto_adjust_count_temp = Gamma_auto_adjust_count;//记录最佳值地址指针

                    Past_Reg_stack[0, Gamma_auto_adjust_count] = input_reg_red;
                    Past_Reg_stack[1, Gamma_auto_adjust_count] = input_reg_green;
                    Past_Reg_stack[2, Gamma_auto_adjust_count] = input_reg_blue;
                    Past_lum_stack[0, Gamma_auto_adjust_count] = measure_x;
                    Past_lum_stack[1, Gamma_auto_adjust_count] = measure_lum;
                    Past_lum_stack[2, Gamma_auto_adjust_count] = measure_y;
                    BCompare0[Gamma_auto_adjust_count] = measure_x + measure_y + measure_lum;

                    if (x_adjust_enable == 1)
                    {
                        if (Math.Abs(measure_x - target_x) > target_x * 20 / 100)
                        {
                            if (target_lum < 1)
                            { step_tmp = Stepm; }
                            else
                            { step_tmp = Steph; }
                        }
                        else if (Math.Abs(measure_x - target_x) > target_x * 2 / 100)
                        {
                            if (target_lum < 1) //if lower target lum
                            { step_tmp = Stepl; }
                            else
                            { step_tmp = Stepm; }
                        }
                        else
                        {
                            step_tmp = Stepl;
                            X_adjust_count++;
                        }
                        if ((Gamma_auto_adjust_count > 5) && (Past_Reg_stack[0, Gamma_auto_adjust_count] == Past_Reg_stack[0, Gamma_auto_adjust_count - 2])) //滤掉第一次采样波动值
                        {
                            step_tmp = step_tmp / 2;
                            if (step_tmp < 1)
                            {
                                step_tmp = Stepl;
                            }
                        }
                        if (measure_x > target_x)
                            output_reg_red = input_reg_red - step_tmp;
                        else output_reg_red = input_reg_red + step_tmp;
                    }
                    //-----------------------------------------------
                    if (lum_adjust_enable == 1)
                    {
                        Y_adjust_count = 0;
                        X_adjust_count = 0;
                        if (Math.Abs(measure_lum - target_lum) > target_lum * 20 / 100)
                        {
                            if (target_lum < 1)
                            { step_tmp = Stepm; }
                            else
                            { step_tmp = Steph; }
                        }
                        else if (Math.Abs(measure_lum - target_lum) > target_lum * 2 / 100)
                        {
                            if (target_lum < 1)
                            { step_tmp = Stepl; }
                            else
                            { step_tmp = Stepm; }
                        }
                        else
                        {
                            step_tmp = Stepl;
                        }
                        //-------------------------step幅值干预---------------------------------
                        if ((gary_target == 255) && (Gamma_auto_adjust_count >= 3) && (Register_lum_jump_flag == false) && (measure_lum > 0)) //滤掉第一次采样波动值
                        {
                            if ((Math.Abs(measure_lum - target_lum) > target_lum * 30 / 100))//无需亮度过调
                            {
                                reg_approach = (int)(Math.Abs((int)(Past_Reg_stack[1, Gamma_auto_adjust_count] - Past_Reg_stack[1, Gamma_auto_adjust_count - 2])) * ((double)(target_lum / Math.Abs((Past_lum_stack[1, Gamma_auto_adjust_count] - Past_lum_stack[1, Gamma_auto_adjust_count - 2])))));
                                if (reg_approach > 0 && reg_approach < 0x3ff)
                                {
                                    step_tmp = reg_approach; //加速亮度逼近目标值
                                }
                            }
                            else
                            {
                                Register_lum_jump_flag = true;//停止加速
                            }
                        }
                        if ((Gamma_auto_adjust_count > 5) && (Past_Reg_stack[1, Gamma_auto_adjust_count] == Past_Reg_stack[1, Gamma_auto_adjust_count - 2]))
                        {
                            step_tmp = step_tmp / 2;
                            if (step_tmp < Stepl)
                            {
                                step_tmp = Stepl;
                            }
                        }
                        if (measure_lum > target_lum)
                            output_reg_green = input_reg_green - step_tmp;
                        else output_reg_green = input_reg_green + step_tmp;
                    }
                    //-----------------------------------------
                    if (y_adjust_enable == 1)
                    {
                        X_adjust_count = 0;
                        {
                            if (Math.Abs(measure_y - target_y) > target_y * 20 / 100)
                            {
                                if (target_lum < 1)
                                { step_tmp = Stepm; }
                                else
                                { step_tmp = Steph; }
                            }
                            else if (Math.Abs(measure_y - target_y) > target_y * 2 / 100)
                            {
                                if (target_lum < 1)
                                { step_tmp = Stepl; }
                                else
                                { step_tmp = Stepm; }
                            }
                            else
                            {
                                step_tmp = Stepl;
                                Y_adjust_count++;
                            }
                            if ((Gamma_auto_adjust_count > 5) && (Past_Reg_stack[2, Gamma_auto_adjust_count] == Past_Reg_stack[2, Gamma_auto_adjust_count - 2])) //滤掉第一次采样波动值
                            {
                                step_tmp = step_tmp / 2;
                                if (step_tmp < 1)
                                {
                                    step_tmp = Stepl;
                                }
                            }
                            if (measure_y > target_y)
                                output_reg_blue = input_reg_blue - step_tmp;
                            else output_reg_blue = input_reg_blue + step_tmp;
                        }
                    }

                    //----------------------------------------------------------------------------
                    if ((Math.Abs(BCompare0[Gamma_auto_adjust_count] - target_reference) < target_reference * (int)(1 - (double)((double)(gary_target / 255) * 0)) / 100))//进入防抖范围
                    {
                        if ((step_tmp == Stepl) || (Stepl == Stepm))
                        {
                            if ((BCompare0[Gamma_auto_adjust_count - 2] > BCompare0[Gamma_auto_adjust_count - 1]) && (Math.Abs(BCompare0[Gamma_auto_adjust_count - 2] - target_reference) < target_reference * (int)(1 - (double)((double)(gary_target / 255) * 0)) / 100) && (Math.Abs(BCompare0[Gamma_auto_adjust_count - 1] - target_reference) < target_reference * (int)(1 - (double)((double)(gary_target / 255) * 0)) / 100))
                            {
                                if (BCompare0[Gamma_auto_adjust_count - 1] > BCompare0[Gamma_auto_adjust_count])
                                {
                                    if ((Y_adjust_count > 3) && (lum_adjust_enable != 1))
                                    // if (((Y_adjust_count > 3)&& (lum_adjust_enable != 1))||((x_adjust_enable!=1)&&((Math.Abs(measure_lum - target_lum) < target_lum * 2 / 100))))
                                    {
                                        if ((Math.Abs(measure_y - target_y) < target_y * 1 / 100) && (Math.Abs(measure_lum - target_lum) < target_lum * 1 / 100))
                                        {
                                            if (measure_y > target_y)
                                            {
                                                output_reg_green = input_reg_green - Stepl;
                                            }
                                            else if (measure_y < target_y)
                                            {
                                                output_reg_green = input_reg_green + Stepl;
                                            }
                                        }
                                        Y_adjust_count = 0;
                                    }
                                    /*
                                    if ((Math.Abs(measure_y - target_y) < target_y * 5 / 1000) && (Math.Abs(measure_x - target_x) < target_x * 5 / 1000) && (Math.Abs(measure_lum - target_lum) < target_lum * 1.5 / 100))
                                    {
                                        Gamma_auto_adjust_count = adjust_times;
                                    }
                                    if ((X_adjust_count > 3) && (y_adjust_enable != 1) && (lum_adjust_enable != 1))
                                    {
                                        if (Math.Abs(measure_x - target_x) < target_x * 7 / 100)
                                        {
                                            X_adjust_count = 0;
                                            Gamma_auto_adjust_count = adjust_times;
                                        }
                                    }
                                    */
                                }
                                else
                                {
                                    output_reg_red = Past_Reg_stack[0, Gamma_auto_adjust_count];
                                    output_reg_green = Past_Reg_stack[1, Gamma_auto_adjust_count];
                                    output_reg_blue = Past_Reg_stack[2, Gamma_auto_adjust_count];
                                    timeout_flag = true;
                                    textBox1.Text = frmHelp.Text + "pass directly!!\r\n";
                                }
                            }
                            else
                            {
                                ;
                            }
                        }
                        else if (step_tmp == Stepm)
                        {
                            Stepm = Stepm / 2;
                            if (Stepm < Stepl)
                            {
                                Stepm = Stepl;
                            }
                            /*
                                                        if ((Y_adjust_count > 3) && (lum_adjust_enable != 1))
                                                        {
                                                            if ((Math.Abs(measure_y - target_y) < target_y * 5 / 100) && (Math.Abs(measure_lum - target_lum) < target_lum * 2 / 100))
                                                            {
                                                                if (measure_y > target_y)
                                                                {
                                                                    output_reg_green = input_reg_green - Stepl;
                                                                }
                                                                else if (measure_y < target_y)
                                                                {
                                                                    output_reg_green = input_reg_green + Stepl;
                                                                }
                                                            }
                                                            Y_adjust_count = 0;
                                                        }
                                                        */
                        }
                        else if (step_tmp == Steph)
                        {
                            Steph = Steph / 2;
                            if (Steph < Stepm)
                            {
                                Steph = Stepm;
                            }
                        }
                    }

                    //----------------------------------若数值超限--------------------------------------                 
                    /*if (((output_reg_red < 30) || (output_reg_red > index2_reg_red) || (output_reg_green < 30) || (output_reg_green > index2_reg_green) || (output_reg_blue < 30) || (output_reg_blue > index2_reg_blue)) && ((target_lum * 95 / 100 < measure_lum) && (target_lum * 105 / 100 > measure_lum)))
                    {
                        output_reg_red = Convert.ToInt16(index1_reg_red - Math.Abs(index2_reg_red - index1_reg_red) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
                        output_reg_green = Convert.ToInt16(index1_reg_green - Math.Abs(index2_reg_green - index1_reg_green) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
                        output_reg_blue = Convert.ToInt16(index1_reg_blue - Math.Abs(index2_reg_blue - index1_reg_blue) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
                    //  textBox1.Text = "该绑点R/G/B插值迭代/r/n";
                        Gamma_auto_adjust_count = 0;
                        timeout_flag = true;
                    }*/
                }
                else
                {
                    Y_adjust_count = 0;
                    // textBox1.Text += "Timeout!\r\n";
                    Gamma_auto_adjust_count = 0;
                    timeout_flag = true;
                    /*
                    if (output_reg_red > index1_reg_red)
                    {
                        output_reg_red = index1_reg_red - Math.Abs(index2_reg_red - index1_reg_red) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
                        // textBox1.Text += "该绑点R差值迭代/r/n";
                    }
                    if (output_reg_green > index1_reg_green)
                    {
                        output_reg_green = index1_reg_green - Math.Abs(index2_reg_green - index1_reg_green) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
                        //  textBox1.Text += "该绑点G差值迭代/r/n";
                    }
                    if (output_reg_blue > index1_reg_blue)
                    {
                        output_reg_blue = index1_reg_blue - Math.Abs(index2_reg_blue - index1_reg_blue) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
                        //  textBox1.Text += "该绑点B差值迭代/r/n";
                    }
                    */
                }
                output_reg_red = Convert.ToInt32(output_reg_red);
                output_reg_green = Convert.ToInt32(output_reg_green);
                output_reg_blue = Convert.ToInt32(output_reg_blue);

                if ((Gamma_auto_adjust_count > 2) && (lum_adjust_enable == 0) && (Register_jump_flag == false)) // only inter once time
                {
                    Register_jump_flag = true; // 只跳转一次
                    reg_reference = Math.Abs(Last_reg_green - Past_Reg_stack[1, Gamma_auto_adjust_count]); //得出差值

                    if (Math.Abs(measure_y - target_y) > target_y * 10 / 100)
                    {
                        if (Last_reg_green > Past_Reg_stack[1, Gamma_auto_adjust_count])
                        {
                            output_reg_blue = Last_reg_blue - reg_reference;
                        }
                        else
                        {
                            output_reg_blue = Last_reg_blue + reg_reference;
                        }
                    }
                    if (Math.Abs(measure_x - target_x) > target_x * 10 / 100)
                    {
                        if (Last_reg_green > Past_Reg_stack[1, Gamma_auto_adjust_count])
                        {
                            output_reg_red = Last_reg_red - reg_reference;
                        }
                        else
                        {
                            output_reg_red = Last_reg_red + reg_reference;
                        }
                    }
                }
            Jump_to_end:;
            }
            catch
            {
                ;
            }
        }
        #endregion
        #region//——————————胡哥算法—————————
        private static bool GammaAdjustTimeoutFlag = false;
        private static int GammaAdjustTimes = 0;
        //--------------------------------------------------------
        private int teppoint = 1; //记录绑点是否变更        
        private int frgb = 1;     //当前调节RGB标志， 1:r;2:G;3:B;

        int step_tmpr = 64, step_tmpg = 64, step_tmpb = 64;//调节初始步长
        bool fdirr = true;
        bool fdirg = true;
        bool fdirb = true;
        private void GammaUpSpeed(int frgb, double targetlum, double nowlum, int nowpoint, int nowRegValue, int lastpoint, int lastRegValue, out int nowRegValueAdj)
        {

            if ((nowRegValue > lastRegValue) && (index > 3))
            {
                nowRegValue = lastRegValue - 1;
            }
            if ((teppoint != nowpoint))
            {
                teppoint = nowpoint;
                step_tmpr = 64;
                step_tmpg = 64;
                step_tmpb = 64;
                if (nowlum > targetlum) //初始化调节方向
                {
                    if (frgb == 1) fdirr = true; //向前瞄准目标数据
                    else if (frgb == 2) fdirg = true;
                    else if (frgb == 3) fdirb = true;
                }
                else
                {
                    if (frgb == 1) fdirr = false;
                    else if (frgb == 2) fdirg = false;
                    else if (frgb == 3) fdirb = false;
                }
            }
            nowRegValueAdj = nowRegValue - 0;

            if (nowlum > targetlum)
            {
                if (frgb == 1)
                {
                    if (!fdirr)//判断前进方向是否瞄准目标点
                    {
                        if (step_tmpr > 1) step_tmpr >>= 1;
                    }
                    nowRegValueAdj = nowRegValue - step_tmpr;
                    fdirr = true;
                }
                else if (frgb == 2)
                {
                    if (!fdirg)
                    {
                        if (step_tmpg > 1) step_tmpg >>= 1;

                    }
                    nowRegValueAdj = nowRegValue - step_tmpg;
                    fdirg = true;
                }
                else if (frgb == 3)
                {
                    if (!fdirr)
                    {
                        if (step_tmpb > 1) step_tmpb >>= 1;
                    }

                    nowRegValueAdj = nowRegValue - step_tmpb;
                    fdirb = true;
                }

            }
            else if (nowlum < targetlum)
            {
                if (frgb == 1)
                {
                    if (fdirr)
                    {
                        if ((step_tmpr > 1)) step_tmpr >>= 1;
                    }
                    nowRegValueAdj = nowRegValue + step_tmpr;
                    fdirr = false;
                }
                else if (frgb == 2)
                {
                    if (fdirg)
                    {
                        if ((step_tmpg > 1)) step_tmpg >>= 1;
                    }
                    nowRegValueAdj = nowRegValue + step_tmpg;
                    fdirg = false;
                }
                else if (frgb == 3)
                {
                    if (fdirb)
                    {
                        if ((step_tmpb > 1)) step_tmpb >>= 1;
                    }
                    nowRegValueAdj = nowRegValue + step_tmpb;
                    fdirb = false;
                }
            }
            frgb = 0;
            if (nowRegValueAdj > 0xffe)
            {
                nowRegValueAdj = 0x7f0;
                textBox1.Text += "Over 0x7FF \r\n";
                textBox1.Select(textBox1.Text.Length, 0);
                textBox1.ScrollToCaret();
            }
            else if (nowRegValueAdj < 1)
            {
                nowRegValueAdj = 10;
                textBox1.Text += "GammaValue <1，检查显示 \r\n";
                textBox1.Select(textBox1.Text.Length, 0);
                textBox1.ScrollToCaret();
            }

        }
        #endregion
        private void TC1105_Others_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[50];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 50;

            Send_buf[0] = 0x56;        //TC1105
            Send_buf[2] = 0x02;
            switch (btnTmp.Name)
            {
                case "btn_Reg_W":         //W写寄存器   1
                    {
                        string strLine = Convert.ToString(txt_Reg_Data_WR.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组

                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x62; //
                        Send_buf[1] = 0x01; //寄存器写
                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                    }
                    break;
                case "btn_Reg_W_Dcs":         //W写寄存器   1
                    {
                        string strLine = Convert.ToString(txt_Reg_Data_WR.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组

                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x62; //
                        Send_buf[1] = 0xa1; //寄存器写
                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                    }
                    break;
                case "btn_Reg_R":         //R读寄存器    2
                    {
                        string strLine = Convert.ToString(txt_Reg_Data_WR.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组
                        int DataNum = Convert.ToByte(txt_return_num_RW.Text);
                        Setting.TempRecord.cmdLen = DataNum + 1 + 5;
                        Send_buf = new byte[DataNum + 1 + 5];

                        Send_buf[0] = 0x62;
                        Send_buf[1] = 0x02; //寄存器读
                        Send_buf[2] = Convert.ToByte(DataNum + 1);//length
                        Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());//register  page
                        Send_buf[4] = byte.Parse((Convert.ToInt32(arr[1], 16)).ToString());//register  addr

                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[Send_buf[2] + 3], out Send_buf[Send_buf[2] + 4]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, Send_buf[2] + 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }

        private void trb_DBV_TrackBar_Scroll(object sender, EventArgs e)
        {
            int bits_51h = Convert.ToInt16(Math.Pow(2, Convert.ToByte(txt_51h_Bits.Text))) - 1;
            lab_trackBar_Display.Text = bits_51h + "阶";
            trb_DBV_TrackBar.Maximum = bits_51h;
            txt_Reg_Data_WR.Text = "ff,78,35,00";
            TC1105_Others_Click(btn_Reg_W, e);
            //txt_Reg_Data_WR.Text = "51,03";//需要重新写入命令
            //TC1105_Others_Click(btn_Reg_W, e);

            if (bits_51h < 0x100)
            {
                txt_Reg_Data_WR.Text = "51," + trb_DBV_TrackBar.Value.ToString("x2");
            }
            else
            {
                txt_Reg_Data_WR.Text = "51" + "," + trb_DBV_TrackBar.Value.ToString("x4").Substring(0, 2) + "," + trb_DBV_TrackBar.Value.ToString("x4").Substring(2, 2);
            }
            TC1105_Others_Click(btn_Reg_W, e);
        }

        #region//-------------------------De-Mura Function--------------------
        private void Demura_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[10];
            Setting.TempRecord.cmdLen = 10;
            Send_buf[0] = 0x2F;
            Send_buf[2] = 0x05;
            Send_buf[3] = 0x20;
            switch (btnTmp.Name)
            {
                case "btn_DeMura_Flash_IDRead":
                    {
                        Send_buf[1] = 0x01;
                    }
                    break;

                case "btn_DeMura_Flash_Setting":
                    {
                        Send_buf[1] = 0x02;
                    }
                    break;

                case "btn_DeMura_Write_Data":
                    {
                        Send_buf[1] = 0x03;

                    }
                    break;

                case "btn_DeMura_RAM_Disable":                   //check  demura data
                    {
                        Send_buf[1] = 0x04;
                    }
                    break;

                case "btn_DeMura_Flash_Erase":
                    {
                        Send_buf[1] = 0x05;
                        MessageBox.Show("擦除时间大约持续50s，请耐心等待擦除完成弹窗。");
                    }
                    break;

                case "btn_DeMura_Flash_Wrting":
                    {
                        Send_buf[1] = 0x06;
                        MessageBox.Show("烧录数据大约需要5s，等待返回De-Mura Data Writing Successful.信息");
                    }
                    break;

                case "btn_DeMura_Reload_Data":
                    {
                        Send_buf[1] = 0x07;
                    }
                    break;

                case "btn_DeMura_Check_CRC":
                    {
                        Send_buf[1] = 0x08;
                    }
                    break;

                case "btn_DeMura_Function_On":
                    {
                        Send_buf[1] = 0x09;
                    }
                    break;

                case "btn_DeMura_Function_Off":
                    {
                        Send_buf[1] = 0x0A;
                    }
                    break;
                default:
                    textBox1.Text = "DeMura 按键异常！！";
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[8], out Send_buf[9]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 10);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
        }

        //测试计算功能
        private void btn_calc_Click(object sender, EventArgs e)
        {
            //===========From initial code===================//
            int Gamma_Point_Num = 23;  //GAMMA 綁點個數
            int[] N1_GammaPoint = new int[23] { 0, 1, 3, 7, 11, 15, 19, 23, 31, 39, 47, 55, 63, 79, 95, 127, 159, 175, 191, 207, 223, 239, 255 }; // GAMMA 綁點位置 0~255, 
            int[][] N1_Gamma_Index = new int[3][];
            //N1_Gamma_Index[0] = new int[24] { 0, 521, 626, 748, 937, 1086, 1213, 1348, 1504, 1620, 1725, 1881, 2072, 2263, 2391, 2501, 2595, 2780, 2997, 3149, 3320, 3536, 3714, 3778 };        //R
            //N1_Gamma_Index[1] = new int[24] { 0, 517, 587, 668, 840, 990, 1118, 1256, 1413, 1528, 1633, 1784, 1973, 2159, 2287, 2397, 2490, 2665, 2876, 3021, 3182, 3383, 3547, 3604 };      //G
            //N1_Gamma_Index[2] = new int[24] { 0, 823, 942, 1066, 1286, 1446, 1557, 1661, 1812, 1933, 2041, 2198, 2382, 2560, 2685, 2791, 2882, 3053, 3251, 3388, 3543, 3736, 3891, 3946 };  //B  

            N1_Gamma_Index[0] = new int[23] {Convert.ToInt16(num_TC1105_R0.Value), Convert.ToInt16(num_TC1105_R1.Value), Convert.ToInt16(num_TC1105_R2.Value), Convert.ToInt16(num_TC1105_R3.Value), Convert.ToInt16(num_TC1105_R4.Value), Convert.ToInt16(num_TC1105_R5.Value), Convert.ToInt16(num_TC1105_R6.Value), Convert.ToInt16(num_TC1105_R7.Value), Convert.ToInt16(num_TC1105_R8.Value), Convert.ToInt16(num_TC1105_R9.Value), Convert.ToInt16(num_TC1105_R10.Value), Convert.ToInt16(num_TC1105_R11.Value),
                                              Convert.ToInt16(num_TC1105_R12.Value), Convert.ToInt16(num_TC1105_R13.Value),Convert.ToInt16(num_TC1105_R14.Value), Convert.ToInt16(num_TC1105_R15.Value), Convert.ToInt16(num_TC1105_R16.Value), Convert.ToInt16(num_TC1105_R17.Value), Convert.ToInt16(num_TC1105_R18.Value), Convert.ToInt16(num_TC1105_R19.Value), Convert.ToInt16(num_TC1105_R20.Value), Convert.ToInt16(num_TC1105_R21.Value), Convert.ToInt16(num_TC1105_R22.Value) };
            N1_Gamma_Index[1] = new int[23] {Convert.ToInt16(num_TC1105_G0.Value), Convert.ToInt16(num_TC1105_G1.Value), Convert.ToInt16(num_TC1105_G2.Value), Convert.ToInt16(num_TC1105_G3.Value), Convert.ToInt16(num_TC1105_G4.Value), Convert.ToInt16(num_TC1105_G5.Value), Convert.ToInt16(num_TC1105_G6.Value), Convert.ToInt16(num_TC1105_G7.Value), Convert.ToInt16(num_TC1105_G8.Value), Convert.ToInt16(num_TC1105_G9.Value), Convert.ToInt16(num_TC1105_G10.Value), Convert.ToInt16(num_TC1105_G11.Value),
                                              Convert.ToInt16(num_TC1105_G12.Value), Convert.ToInt16(num_TC1105_G13.Value),Convert.ToInt16(num_TC1105_G14.Value), Convert.ToInt16(num_TC1105_G15.Value), Convert.ToInt16(num_TC1105_G16.Value), Convert.ToInt16(num_TC1105_G17.Value), Convert.ToInt16(num_TC1105_G18.Value), Convert.ToInt16(num_TC1105_G19.Value), Convert.ToInt16(num_TC1105_G20.Value), Convert.ToInt16(num_TC1105_G21.Value), Convert.ToInt16(num_TC1105_G22.Value) };                                                                                                                                                                             //N1_GammaIndex[0][Gamma_Point_Num]為R的GAMMA INDEX, N1_GammaIndex[1][Gamma_Point_Num]為G的GAMMA INDEX,N1_GammaIndex[2][Gamma_Point_Num]為B的GAMMA INDEX

            N1_Gamma_Index[2] = new int[23] {Convert.ToInt16(num_TC1105_B0.Value), Convert.ToInt16(num_TC1105_B1.Value), Convert.ToInt16(num_TC1105_B2.Value), Convert.ToInt16(num_TC1105_B3.Value), Convert.ToInt16(num_TC1105_B4.Value), Convert.ToInt16(num_TC1105_B5.Value), Convert.ToInt16(num_TC1105_B6.Value), Convert.ToInt16(num_TC1105_B7.Value), Convert.ToInt16(num_TC1105_B8.Value), Convert.ToInt16(num_TC1105_B9.Value), Convert.ToInt16(num_TC1105_B10.Value), Convert.ToInt16(num_TC1105_B11.Value),
                                              Convert.ToInt16(num_TC1105_B12.Value), Convert.ToInt16(num_TC1105_B13.Value),Convert.ToInt16(num_TC1105_B14.Value), Convert.ToInt16(num_TC1105_B15.Value), Convert.ToInt16(num_TC1105_B16.Value), Convert.ToInt16(num_TC1105_B17.Value), Convert.ToInt16(num_TC1105_B18.Value), Convert.ToInt16(num_TC1105_B19.Value), Convert.ToInt16(num_TC1105_B20.Value), Convert.ToInt16(num_TC1105_B21.Value), Convert.ToInt16(num_TC1105_B22.Value)};


            //int N2_DBV = 281; // read register : P9_AD_AE
            int N2_DBV = Convert.ToInt16(txt_Calc.Text);

            //===========From initial code=============//

            int i, j, k;
            double[] N2_Table = new double[23];
            double Ratio = Math.Pow(Convert.ToDouble(N2_DBV / 1023.0), 1 / 2.2);
            int[][] N2_Gamma_Value = new int[3][];
            N2_Gamma_Value[0] = new int[23];
            N2_Gamma_Value[1] = new int[23];
            N2_Gamma_Value[2] = new int[23];

            for (i = 0; i < Gamma_Point_Num; i++)
            {
                N2_Table[i] = N1_GammaPoint[i] * Ratio;
            }


            for (i = 0; i < Gamma_Point_Num; i++)
            {
                for (j = 0; j < (Gamma_Point_Num - 1); j++)
                {
                    if (N2_Table[i] >= N1_GammaPoint[j] && N2_Table[i] <= N1_GammaPoint[j + 1])
                    {
                        for (k = 0; k < 3; k++)
                        {
                            N2_Gamma_Value[k][i] = Convert.ToInt16(((N2_Table[i] - N1_GammaPoint[j]) * N1_Gamma_Index[k][j + 1] + (N1_GammaPoint[j + 1] - N2_Table[i]) * N1_Gamma_Index[k][j]) / (N1_GammaPoint[j + 1] - (float)N1_GammaPoint[j]));
                        }
                        break;
                    }
                }
            }
            {
                {
                    num_TC1105_R0.Value = Convert.ToDecimal(N2_Gamma_Value[0][0]);
                    num_TC1105_R1.Value = Convert.ToDecimal(N2_Gamma_Value[0][1]);
                    num_TC1105_R2.Value = Convert.ToDecimal(N2_Gamma_Value[0][2]);
                    num_TC1105_R3.Value = Convert.ToDecimal(N2_Gamma_Value[0][3]);
                    num_TC1105_R4.Value = Convert.ToDecimal(N2_Gamma_Value[0][4]);
                    num_TC1105_R5.Value = Convert.ToDecimal(N2_Gamma_Value[0][5]);
                    num_TC1105_R6.Value = Convert.ToDecimal(N2_Gamma_Value[0][6]);
                    num_TC1105_R7.Value = Convert.ToDecimal(N2_Gamma_Value[0][7]);
                    num_TC1105_R8.Value = Convert.ToDecimal(N2_Gamma_Value[0][8]);
                    num_TC1105_R9.Value = Convert.ToDecimal(N2_Gamma_Value[0][9]);
                    num_TC1105_R10.Value = Convert.ToDecimal(N2_Gamma_Value[0][10]);
                    num_TC1105_R11.Value = Convert.ToDecimal(N2_Gamma_Value[0][11]);

                    num_TC1105_R12.Value = Convert.ToDecimal(N2_Gamma_Value[0][12]);
                    num_TC1105_R13.Value = Convert.ToDecimal(N2_Gamma_Value[0][13]);
                    num_TC1105_R14.Value = Convert.ToDecimal(N2_Gamma_Value[0][14]);
                    num_TC1105_R15.Value = Convert.ToDecimal(N2_Gamma_Value[0][15]);
                    num_TC1105_R16.Value = Convert.ToDecimal(N2_Gamma_Value[0][16]);
                    num_TC1105_R17.Value = Convert.ToDecimal(N2_Gamma_Value[0][17]);
                    num_TC1105_R18.Value = Convert.ToDecimal(N2_Gamma_Value[0][18]);
                    num_TC1105_R19.Value = Convert.ToDecimal(N2_Gamma_Value[0][19]);
                    num_TC1105_R20.Value = Convert.ToDecimal(N2_Gamma_Value[0][20]);
                    num_TC1105_R21.Value = Convert.ToDecimal(N2_Gamma_Value[0][21]);
                    num_TC1105_R22.Value = Convert.ToDecimal(N2_Gamma_Value[0][22]);
                    //num_TC1105_R23.Value = Convert.ToDecimal(N2_Gamma_Value[0][23]);
                }
            }
            {
                num_TC1105_G0.Value = Convert.ToDecimal(N2_Gamma_Value[1][0]);
                num_TC1105_G1.Value = Convert.ToDecimal(N2_Gamma_Value[1][1]);
                num_TC1105_G2.Value = Convert.ToDecimal(N2_Gamma_Value[1][2]);
                num_TC1105_G3.Value = Convert.ToDecimal(N2_Gamma_Value[1][3]);
                num_TC1105_G4.Value = Convert.ToDecimal(N2_Gamma_Value[1][4]);
                num_TC1105_G5.Value = Convert.ToDecimal(N2_Gamma_Value[1][5]);
                num_TC1105_G6.Value = Convert.ToDecimal(N2_Gamma_Value[1][6]);
                num_TC1105_G7.Value = Convert.ToDecimal(N2_Gamma_Value[1][7]);
                num_TC1105_G8.Value = Convert.ToDecimal(N2_Gamma_Value[1][8]);
                num_TC1105_G9.Value = Convert.ToDecimal(N2_Gamma_Value[1][9]);
                num_TC1105_G10.Value = Convert.ToDecimal(N2_Gamma_Value[1][10]);
                num_TC1105_G11.Value = Convert.ToDecimal(N2_Gamma_Value[1][11]);

                num_TC1105_G12.Value = Convert.ToDecimal(N2_Gamma_Value[1][12]);
                num_TC1105_G13.Value = Convert.ToDecimal(N2_Gamma_Value[1][13]);
                num_TC1105_G14.Value = Convert.ToDecimal(N2_Gamma_Value[1][14]);
                num_TC1105_G15.Value = Convert.ToDecimal(N2_Gamma_Value[1][15]);
                num_TC1105_G16.Value = Convert.ToDecimal(N2_Gamma_Value[1][16]);
                num_TC1105_G17.Value = Convert.ToDecimal(N2_Gamma_Value[1][17]);
                num_TC1105_G18.Value = Convert.ToDecimal(N2_Gamma_Value[1][18]);
                num_TC1105_G19.Value = Convert.ToDecimal(N2_Gamma_Value[1][19]);
                num_TC1105_G20.Value = Convert.ToDecimal(N2_Gamma_Value[1][20]);
                num_TC1105_G21.Value = Convert.ToDecimal(N2_Gamma_Value[1][21]);
                num_TC1105_G22.Value = Convert.ToDecimal(N2_Gamma_Value[1][22]);
                //num_TC1105_G23.Value = Convert.ToDecimal(N2_Gamma_Value[1][23]);
            }
            {
                num_TC1105_B0.Value = Convert.ToDecimal(N2_Gamma_Value[2][0]);
                num_TC1105_B1.Value = Convert.ToDecimal(N2_Gamma_Value[2][1]);
                num_TC1105_B2.Value = Convert.ToDecimal(N2_Gamma_Value[2][2]);
                num_TC1105_B3.Value = Convert.ToDecimal(N2_Gamma_Value[2][3]);
                num_TC1105_B4.Value = Convert.ToDecimal(N2_Gamma_Value[2][4]);
                num_TC1105_B5.Value = Convert.ToDecimal(N2_Gamma_Value[2][5]);
                num_TC1105_B6.Value = Convert.ToDecimal(N2_Gamma_Value[2][6]);
                num_TC1105_B7.Value = Convert.ToDecimal(N2_Gamma_Value[2][7]);
                num_TC1105_B8.Value = Convert.ToDecimal(N2_Gamma_Value[2][8]);
                num_TC1105_B9.Value = Convert.ToDecimal(N2_Gamma_Value[2][9]);
                num_TC1105_B10.Value = Convert.ToDecimal(N2_Gamma_Value[2][10]);
                num_TC1105_B11.Value = Convert.ToDecimal(N2_Gamma_Value[2][11]);

                num_TC1105_B12.Value = Convert.ToDecimal(N2_Gamma_Value[2][12]);
                num_TC1105_B13.Value = Convert.ToDecimal(N2_Gamma_Value[2][13]);
                num_TC1105_B14.Value = Convert.ToDecimal(N2_Gamma_Value[2][14]);
                num_TC1105_B15.Value = Convert.ToDecimal(N2_Gamma_Value[2][15]);
                num_TC1105_B16.Value = Convert.ToDecimal(N2_Gamma_Value[2][16]);
                num_TC1105_B17.Value = Convert.ToDecimal(N2_Gamma_Value[2][17]);
                num_TC1105_B18.Value = Convert.ToDecimal(N2_Gamma_Value[2][18]);
                num_TC1105_B19.Value = Convert.ToDecimal(N2_Gamma_Value[2][19]);
                num_TC1105_B20.Value = Convert.ToDecimal(N2_Gamma_Value[2][20]);
                num_TC1105_B21.Value = Convert.ToDecimal(N2_Gamma_Value[2][21]);
                num_TC1105_B22.Value = Convert.ToDecimal(N2_Gamma_Value[2][22]);
                //num_TC1105_B23.Value = Convert.ToDecimal(N2_Gamma_Value[2][23]);
            }

        }

        private void Demura_Click_Simplify(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[10];
            Setting.TempRecord.cmdLen = 10;
            Send_buf[0] = 0x2F;
            Send_buf[2] = 0x05;
            Send_buf[3] = 0x1F;
            switch (btnTmp.Name)
            {
                case "btn_DeMura_EN":
                    {
                        Send_buf[1] = 0x01; //0x06
                    }
                    break;

                case "btn_DeMura_Erase":
                    {
                        Send_buf[1] = 0x05;
                    }
                    break;

                case "btn_DeMura_Into":
                    {
                        //Send_buf[1] = 0x01;
                        PreLoad_Demura_data_Click();
                    }
                    break;

                case "btn_DeMura_Enter":
                    {
                        Send_buf[1] = 0x04;
                    }
                    break;

                case "btn_DeMura_Wrting":
                    {
                        Send_buf[1] = 0x06;
                        MessageBox.Show("烧录数据大约需要5s，等待返回De-Mura Data Writing Successful.信息");
                    }
                    break;

                case "btn_DeMura_Reload_Data":
                    {

                        Send_buf[1] = 0x07;
                    }
                    break;
                case "btn_DeMura_Function_On":
                    {
                        Send_buf[1] = 0x09;
                    }
                    break;

                case "btn_DeMura_Function_Off":
                    {
                        Send_buf[1] = 0x0A;
                    }
                    break;
                default:
                    textBox1.Text = "DeMura 按键异常！！";
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[8], out Send_buf[9]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 10);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
        }

        //特殊区域有惊喜
        string[] bmpname = new string[24] {"A_R32", "A_R64", "A_R96", "A_R128", "A_R160", "A_R192", "A_R224", "A_R255",
                                           "A_G32", "A_G64", "A_G96", "A_G128", "A_G160", "A_G192", "A_G224", "A_G255",
                                           "A_B32", "A_B64", "A_B96", "A_B128", "A_B160", "A_B192", "A_B224", "A_B255"};

        private void Specbuttonsll_Click(object sender, EventArgs e)
        {
            //变量
            bool load_bmp_status = false;
            for (int i = 0; i < bmpname.Length; i++)
            {
                string bmppath = "./testbmp/" + bmpname[i] + ".bmp";

                if (!File.Exists(bmppath))
                {
                    MessageBox.Show(bmpname[i] + "图片不存在");
                    return;
                }

                load_bmp_status = loadbmpfile(bmppath);
                if (!load_bmp_status)
                {
                    MessageBox.Show(bmpname[i] + "图片加载失败");
                    return;
                }
                textBox2.Text = bmpname[i];
                //310采集数据
                if ((i == 8) || (i == 16) || (i == 0))
                {
                    Thread.Sleep(100);
                }
                else
                {
                    Thread.Sleep(50);
                }

                frmHelp.Msr_CASDK();
                reallum[i] = frmHelp.CASDK_LUM;
            }
            if (this.radiobtnMain.Checked == true)
            {
                for (int i = 0; i < 24; i++)
                {
                    reallumMain[i] = reallum[i];
                }
            }
            else if (this.radioBtnSub.Checked == true)
            {
                for (int i = 0; i < 24; i++)
                {
                    reallumSub[i] = reallum[i];
                }
            }
            else
            {
                MessageBox.Show("未选择主副屏按钮!!!");
            }


        }





        //gamma 1.9 2.2 2.5计算
        private void standradgamma()
        {
            //gamma 曲线数据
            double[] theorygamma = new double[] { Math.Round(32 / (255 * 1.0), 3), Math.Round(64 / (255 * 1.0), 3), Math.Round((96 / (255 * 1.0)), 3), Math.Round((128 / (255 * 1.0)), 3), Math.Round((160 / (255 * 1.0)), 3), Math.Round((192 / (255 * 1.0)), 3), Math.Round((224 / (255 * 1.0)), 3), Math.Round((255 / (255 * 1.0)), 3) };
            gamma22 = new double[] { Math.Round((Math.Pow(theorygamma[0], 2.2) * 1.0), 3), Math.Pow(theorygamma[1], 2.2), Math.Pow(theorygamma[2], 2.2), Math.Pow(theorygamma[3], 2.2), Math.Pow(theorygamma[4], 2.2), Math.Pow(theorygamma[5], 2.2), Math.Pow(theorygamma[6], 2.2), Math.Pow(theorygamma[7], 2.2) };
            gamma19 = new double[] { Math.Pow(theorygamma[0], 1.9), Math.Pow(theorygamma[1], 1.9), Math.Pow(theorygamma[2], 1.9), Math.Pow(theorygamma[3], 1.9), Math.Pow(theorygamma[4], 1.9), Math.Pow(theorygamma[5], 1.9), Math.Pow(theorygamma[6], 1.9), Math.Pow(theorygamma[7], 1.9) };
            gamma25 = new double[] { Math.Pow(theorygamma[0], 2.5), Math.Pow(theorygamma[1], 2.5), Math.Pow(theorygamma[2], 2.5), Math.Pow(theorygamma[3], 2.5), Math.Pow(theorygamma[4], 2.5), Math.Pow(theorygamma[5], 2.5), Math.Pow(theorygamma[6], 2.5), Math.Pow(theorygamma[7], 2.5) };

            //--------------------------
            for (int i = 0; i < 8; i++)
            {
                gamma22[i] = Math.Round(gamma22[i], 2);
                gamma19[i] = Math.Round(gamma19[i], 2);
                gamma25[i] = Math.Round(gamma25[i], 2);
            }
        }



        //gdi 画图算法
        private void drawpiclowgamma(double[] ration_lvRGB, int shownum, int gammanum)
        {
            string[] month = new string[8] { "32", "64", "96", "128", "160", "192", "224", "255" };
            //求出纵坐标的范围
            double[] ynum = new double[5];
            ynum[0] = 0.2;
            ynum[1] = 0.4;
            ynum[2] = 0.6;
            ynum[3] = 0.8;
            ynum[4] = 1.0;
            double kedunum = 0.2;

            //做竖坐标的比值处理
            for (int i = 0; i < 8; i++)
            {
                ration_lvRGB[i] = ration_lvRGB[i] / ration_lvRGB[7];
            }

            //定义上一次坐标
            float x_oldPoint = 0.00f, y_oldPoint = 0.00f;

            //画图初始化
            Bitmap bmap = new Bitmap(500, 500);
            Graphics gph = Graphics.FromImage(bmap);
            gph.Clear(Color.White);



            //用于标题显示
            string str_shownum = "";
            switch (shownum)
            {
                case 1:
                    str_shownum = "R";
                    break;
                case 2:
                    str_shownum = "G";
                    break;
                case 3:
                    str_shownum = "B";
                    break;
            }
            PointF cpt = new PointF(40, 420);//中心点
            PointF[] xpt = new PointF[3] { new PointF(cpt.Y + 15, cpt.Y), new PointF(cpt.Y, cpt.Y - 8), new PointF(cpt.Y, cpt.Y + 8) };//x轴三角形
            PointF[] ypt = new PointF[3] { new PointF(cpt.X, cpt.X - 15), new PointF(cpt.X - 8, cpt.X), new PointF(cpt.X + 8, cpt.X) };//y轴三角形

            if (gammanum == 1)
            {
                gph.DrawString("主屏的( " + str_shownum + " )Gamma曲线", new Font("宋体", 10), Brushes.Black, new PointF(cpt.X + 180, cpt.X - 15));//图表标题
            }
            else if (gammanum == 2)
            {
                gph.DrawString("副屏的( " + str_shownum + " )Gamma曲线", new Font("宋体", 10), Brushes.Black, new PointF(cpt.X + 180, cpt.X - 15));//图表标题
            }


            //画x轴
            gph.DrawLine(Pens.Black, cpt.X, cpt.Y, cpt.Y, cpt.Y);
            gph.DrawPolygon(Pens.Black, xpt);
            gph.FillPolygon(new SolidBrush(Color.Black), xpt);
            gph.DrawString("灰阶", new Font("宋体", 10), Brushes.Black, new PointF(cpt.Y + 10, cpt.Y + 10));
            //画y轴
            gph.DrawLine(Pens.Black, cpt.X, cpt.Y, cpt.X, cpt.X);
            gph.DrawPolygon(Pens.Black, ypt);
            gph.FillPolygon(new SolidBrush(Color.Black), ypt);
            gph.DrawString("值", new Font("宋体", 12), Brushes.Black, new PointF(0, 5));
            for (int j = 1; j <= 5; j++)
            {
                //画y轴刻度
                gph.DrawString((ynum[j - 1]).ToString(), new Font("宋体", 8), Brushes.Black, new PointF(cpt.X - 26, cpt.Y - j * 62 - 6));
                gph.DrawLine(Pens.Black, cpt.X - 6, cpt.Y - j * 62, cpt.X, cpt.Y - j * 62);

            }

            for (int jj = 0; jj < 4; jj++)
            {
                switch (jj)
                {
                    case 0:
                        break;
                    case 1:
                        ration_lvRGB = gamma19;
                        break;
                    case 2:
                        ration_lvRGB = gamma22;
                        break;
                    case 3:
                        ration_lvRGB = gamma25;
                        break;

                }

                for (int i = 1; i <= 8; i++)
                {


                    //画x轴项目
                    if (month[i - 1].Length > 2)
                    {
                        gph.DrawString(month[i - 1], new Font("宋体", 8), Brushes.Black, new PointF(cpt.X + i * 43 - 12, cpt.Y + 5));
                    }
                    else
                    {
                        gph.DrawString(month[i - 1], new Font("宋体", 8), Brushes.Black, new PointF(cpt.X + i * 43 - 9, cpt.Y + 5));
                    }

                    gph.DrawLine(Pens.Black, cpt.X + i * 43 - 5, cpt.Y, cpt.X + i * 43 - 5, cpt.Y - 3);
                    // gph.DrawString(month[i - 1].Substring(1, 1), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 45 - 5, cpt.Y + 20));
                    // if (month[i - 1].Length > 2) gph.DrawString(month[i - 1].Substring(2, 1), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 45 - 5, cpt.Y + 35));

                    //画点
                    if (jj == 0)
                    {
                        gph.DrawEllipse(Pens.Red, cpt.X + i * 43 - 5 - 3, cpt.Y - (float)((ration_lvRGB[i - 1] - ynum[0]) / kedunum + 1) * 62 - 3, 6, 6);
                        gph.FillEllipse(new SolidBrush(Color.Red), cpt.X + i * 43 - 5 - 3, cpt.Y - (float)((ration_lvRGB[i - 1] - ynum[0]) / kedunum + 1) * 62 - 3, 6, 6);
                    }

                    //画数值
                    //gph.DrawString(ration_lvRGB[i - 1].ToString(), new Font("宋体", 8), Brushes.Black, new PointF(cpt.X + i * 27 - 5, cpt.Y - (float)((ration_lvRGB[i - 1] - ynum[0]) / kedunum + 1) * 39 + 6));
                    //画折线
                    Pen pen1 = new Pen(Color.Red, 2);
                    if (jj == 1)
                    {
                        pen1 = new Pen(Color.Black, 2);
                        if (i > 1) gph.DrawLine(pen1, x_oldPoint, y_oldPoint, cpt.X + i * 43 - 5, cpt.Y - (float)((ration_lvRGB[i - 1] - ynum[0]) / kedunum + 1) * 62);
                    }
                    else if (jj == 2)
                    {
                        pen1 = new Pen(Color.Gray, 2);
                        if (i > 1) gph.DrawLine(pen1, x_oldPoint, y_oldPoint, cpt.X + i * 43 - 5, cpt.Y - (float)((ration_lvRGB[i - 1] - ynum[0]) / kedunum + 1) * 62);
                    }
                    else if (jj == 3)
                    {
                        pen1 = new Pen(Color.Blue, 2);
                        if (i > 1) gph.DrawLine(pen1, x_oldPoint, y_oldPoint, cpt.X + i * 43 - 5, cpt.Y - (float)((ration_lvRGB[i - 1] - ynum[0]) / kedunum + 1) * 62);
                    }
                    else if (jj == 0)
                    {
                        pen1 = new Pen(Color.Red, 2);
                        pen1.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虚线
                        if (i > 1) gph.DrawLine(pen1, x_oldPoint, y_oldPoint, cpt.X + i * 43 - 5, cpt.Y - (float)((ration_lvRGB[i - 1] - ynum[0]) / kedunum + 1) * 62);
                    }

                    //对当次坐标进行赋值，为下次划线做准备
                    x_oldPoint = cpt.X + i * 43 - 5;
                    y_oldPoint = cpt.Y - (float)((ration_lvRGB[i - 1] - ynum[0]) / kedunum + 1) * 62;

                }
            }

            gph.DrawString("gamma1.9", new Font("宋体", 8), Brushes.Black, new PointF(cpt.X + 45, cpt.X));
            Pen pen2 = new Pen(Color.Black, 5);
            gph.DrawLine(pen2, cpt.X + 10, cpt.X + 6, cpt.X + 40, cpt.X + 6);
            pen2 = new Pen(Color.Gray, 5);
            gph.DrawLine(pen2, cpt.X + 10, cpt.X + 16, cpt.X + 40, cpt.X + 16);
            gph.DrawString("gamma2.2", new Font("宋体", 8), Brushes.Black, new PointF(cpt.X + 45, cpt.X + 10));
            pen2 = new Pen(Color.Blue, 5);
            gph.DrawLine(pen2, cpt.X + 10, cpt.X + 26, cpt.X + 40, cpt.X + 26);
            gph.DrawString("gamma2.5", new Font("宋体", 8), Brushes.Black, new PointF(cpt.X + 45, cpt.X + 20));
            pen2 = new Pen(Color.Red, 5);
            pen2.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虚线
            gph.DrawLine(pen2, cpt.X + 10, cpt.X + 36, cpt.X + 40, cpt.X + 36);
            gph.DrawString("real-gamma", new Font("宋体", 8), Brushes.Black, new PointF(cpt.X + 45, cpt.X + 30));

            //保存输出图片
            //bmap.Save(Response.OutputStream, ImageFormat.Gif);
            if (gammanum == 1)
            {
                pictureBox1.Image = bmap;
            }
            else if (gammanum == 2)
            {
                pictureBox2.Image = bmap;
            }
        }



        private bool loadbmpfile(string bmppath)
        {
            Image imgBack = System.Drawing.Image.FromFile(bmppath);
            Bitmap loadbmp = (Bitmap)imgBack;
            MemoryStream aFile;
            aFile = new MemoryStream();
            loadbmp.Save(aFile, System.Drawing.Imaging.ImageFormat.Bmp);
            aFile.Seek(0, SeekOrigin.Begin);//            aFile.Position = 0;             
            Int32 HACT_True;
            Int32 VACT_True;
            Int32 m_txlen = 0;
            Int32 j;
            //FileStream aFile;
            byte[] TemFileContent = new byte[1024 * 100];
            System.Int32 ByteReaded = 0x00;
            #region
            //------------20180808---------------
            try
            { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                frmHelp.comPort.Write(TemFileContent, 0, 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                imgBack.Dispose();
                loadbmp.Dispose();
                aFile.Dispose();
                return false;
            }
            //-----------------------------------
            #endregion
            try
            {
                ByteReaded = aFile.Read(TemFileContent, 0, 54);
                HACT_True = (Convert.ToInt32(TemFileContent[19]) << 8) + TemFileContent[18];
                VACT_True = Convert.ToInt32((TemFileContent[23] << 8) + TemFileContent[22]);
                j = 1;
                {
                    aFile.Seek(54, SeekOrigin.Begin);
                    while (j <= VACT_True)
                    {
                        if (HACT_True % 4 != 0)
                            aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                        else
                            aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);

                        ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 
                        try
                        {
                            frmHelp.comPort.Write(TemFileContent, 0, ByteReaded);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            imgBack.Dispose();
                            loadbmp.Dispose();
                            aFile.Dispose();
                            break;
                        }
                        m_txlen += ByteReaded;
                        progressBar2.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                        label1.Text = progressBar2.Value.ToString() + "%";
                        // Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                        j++;
                    }
                    TemFileContent[0] = 0x5A;
                    try
                    {
                        frmHelp.comPort.Write(TemFileContent, 0, 1);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        imgBack.Dispose();
                        loadbmp.Dispose();
                        aFile.Dispose();
                        return false;
                    }
                    aFile.Close();
                }
                imgBack.Dispose();
                loadbmp.Dispose();
                aFile.Dispose();
                return true;
            }
            catch
            {
                textBox1.Text += "啥图片都没显示";
                textBox1.Select(textBox1.Text.Length, 0);
                textBox1.ScrollToCaret();
                imgBack.Dispose();
                loadbmp.Dispose();
                aFile.Dispose();
                return false;
            }

        }



        //画曲线的响应函数
        private void drawpic_Click(object sender, EventArgs e)
        {
            groupBox3.Visible = true;
            standradgamma();
            if (radiobtnMain.Checked == true)
            {
                if (comboBox1.SelectedItem.ToString() == "R")
                {
                    for (int i = 0; i < 8; i++)
                    {
                        realnumtemp[i] = reallumMain[i];
                    }
                    drawpiclowgamma(realnumtemp, 1, 1);
                }
                else if (comboBox1.SelectedItem.ToString() == "G")
                {
                    for (int i = 0; i < 8; i++)
                    {
                        realnumtemp[i] = reallumMain[i + 8];
                    }
                    drawpiclowgamma(realnumtemp, 2, 1);
                }
                else if (comboBox1.SelectedItem.ToString() == "B")
                {
                    for (int i = 0; i < 8; i++)
                    {
                        realnumtemp[i] = reallumMain[i + 16];
                    }
                    drawpiclowgamma(realnumtemp, 3, 1);
                }

            }
            else if (radioBtnSub.Checked == true)
            {
                if (comboBox1.SelectedItem.ToString() == "R")
                {
                    for (int i = 0; i < 8; i++)
                    {
                        realnumtemp[i] = reallumSub[i];
                    }
                    drawpiclowgamma(realnumtemp, 1, 2);
                }
                else if (comboBox1.SelectedItem.ToString() == "G")
                {
                    for (int i = 0; i < 8; i++)
                    {
                        realnumtemp[i] = reallumSub[i + 8];
                    }
                    drawpiclowgamma(realnumtemp, 2, 2);
                }
                else if (comboBox1.SelectedItem.ToString() == "B")
                {
                    for (int i = 0; i < 8; i++)
                    {
                        realnumtemp[i] = reallumSub[i + 16];
                    }
                    drawpiclowgamma(realnumtemp, 3, 2);
                }
            }
            else
            {
                MessageBox.Show("未选择主副屏按钮!!!");
            }

        }

        //显示画图区域
        private void showpic_Click(object sender, EventArgs e)
        {
            groupBox3.Visible = true;
        }

        //隐藏画图区域
        private void hidepic_Click(object sender, EventArgs e)
        {
            groupBox3.Visible = false;
        }


        //解析demura配置文件
        private void pre_DeMura_Page_Data_Click(object sender, EventArgs e)
        {
            int File_Length_pre = 0;
            byte[] TemFileContentpre = new byte[200];
            System.Int32 ByteReaded = 0x00;
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "文本文件(*.bin)|*.bin|所有文件(*.*)|*.*";
            of.Title = "初始化 Code";
            if (of.ShowDialog() == DialogResult.OK)
            {
                FileStream aFile = new FileStream(of.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                File_Length_pre = Convert.ToInt32(aFile.Length);    //

                //进入相应位置
                try
                { //Demura 告诉下位机Demura数据搬运开始
                    TemFileContentpre[0] = 0xbd;
                    TemFileContentpre[1] = 0xcf;
                    TemFileContentpre[2] = 0xdf;
                    TemFileContentpre[3] = 0xef;
                    TemFileContentpre[4] = 0xff;
                    TemFileContentpre[5] = Convert.ToByte(File_Length_pre & 0xff);
                    frmHelp.comPort.Write(TemFileContentpre, 0, 6);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }

                //txt_Reg_Data_WR.Text = "df,ec,7e,11";
                //TC1105_Others_Click(btn_Reg_W, e);

                aFile.Seek(0, SeekOrigin.Begin);    //跳转
                ByteReaded = aFile.Read(TemFileContentpre, 0, File_Length_pre);
                //for (int i = 0; i < File_Length_pre; i++)
                //{
                //    //TemFileContentpre[i] = Convert.ToByte(int.Parse(Encoding.ASCII.GetString(TemFileContentpre).Substring(i, 2), NumberStyles.AllowHexSpecifier));
                //}
                try
                {
                    frmHelp.comPort.Write(TemFileContentpre, 0, ByteReaded);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }

                textBox1.Text += "demura配置文件开始写入 \r\n";
            }
        }

        private void PreLoad_Demura_data_Click()  //预加载Demura文件
        {
            Int32 m_txlen = 0;
            Int32 Once_txlen = 0;
            Int32 File_Length = 0, File_Length_tep = 0, File_Temp_length = 0;
            bool tx_en = true;
            byte[] TemFileContent = new byte[1024 * 100];
            char[] Acsii_array = new char[2];
            Int32 div = 0;

            // div = Convert.ToInt16(frmHelp.HACT.Value) * 3;
            div = 240;
            System.Int32 ByteReaded = 0x00;
            {
                OpenFileDialog of = new OpenFileDialog();
                of.Filter = "文本文件(*.bin)|*.bin|所有文件(*.*)|*.*";
                of.Title = "初始化 Code";
                if (of.ShowDialog() == DialogResult.OK)
                {
                    FileStream aFile = new FileStream(of.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    File_Length = Convert.ToInt32(aFile.Length);    //发送最开始是加上 01
                                                                    // File_Length = 15;
                    File_Length_tep = File_Length;
                    File_Temp_length = File_Length;
                    textBox1.Text += "开始搬运 demura data \r\n";
                    try
                    { //Demura 告诉下位机Demura数据搬运开始
                        TemFileContent[0] = 0xbd;
                        TemFileContent[1] = 0x5B;
                        TemFileContent[2] = 0xb5;
                        TemFileContent[3] = 0xdd;
                        TemFileContent[4] = 0xbb;
                        TemFileContent[5] = Convert.ToByte(File_Temp_length >> 24);
                        TemFileContent[6] = Convert.ToByte((File_Temp_length >> 16) & 0xff);
                        TemFileContent[7] = Convert.ToByte((File_Temp_length >> 8) & 0xff);
                        TemFileContent[8] = Convert.ToByte(File_Temp_length & 0xff);
                        frmHelp.comPort.Write(TemFileContent, 0, 9);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        tx_en = false;
                        return;
                    }

                    while (tx_en)
                    {
                        if (File_Length_tep > (File_Length % div))
                        {
                            Once_txlen = div;
                        }
                        else  //最后一笔
                        {
                            Once_txlen = Convert.ToInt32(aFile.Length % div);
                            tx_en = false;
                        }
                        aFile.Seek(0 + m_txlen, SeekOrigin.Begin);    //跳转
                        ByteReaded = aFile.Read(TemFileContent, 0, Once_txlen);
                        try
                        {
                            //TemFileContent[0] = 0x30;
                            //TemFileContent[1] = 0x31;
                            //TemFileContent[2] = 0x32;
                            //TemFileContent[3] = 0x33;
                            //TemFileContent[4] = 0x34;
                            //TemFileContent[5] = 0x35;
                            //TemFileContent[6] = 0x36;
                            //TemFileContent[7] = 0x37;
                            //TemFileContent[8] = 0x38;
                            //TemFileContent[9] = 0x39;
                            //TemFileContent[10] = 0x3a;
                            //TemFileContent[11] = 0x3b;
                            //TemFileContent[12] = 0x3C;
                            //TemFileContent[13] = 0x3d;
                            //TemFileContent[14] = 0x3e;
                            frmHelp.comPort.Write(TemFileContent, 0, ByteReaded + 0);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            tx_en = false;
                            break;
                        }
                        m_txlen += ByteReaded;
                        progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                        label_flash.Text = progressBar1.Value.ToString() + "%";
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                        File_Length_tep = File_Length_tep - div;
                    }
                    aFile.Close();

                }
                else
                {
                    MessageBox.Show("请选择文件夹", "错误");
                }
            }

        }
        #endregion



        private void TC1105_OTP_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[8];
            Setting.TempRecord.cmdLen = 8;
            Send_buf[0] = 0x56; //TC1105Gamma
            Send_buf[1] = 0x03;
            Send_buf[2] = 3;
            //if (rbt_TC1105_Normal_1_OTP.Checked == true)
            //{ Send_buf[3] = 0x10; }
            //else if (rbt_TC1105_Normal_2_OTP.Checked == true)
            //{ Send_buf[3] = 0x20; }
            //else if (rbt_TC1105_Normal_3_OTP.Checked == true)
            //{ Send_buf[3] = 0x30; }
            //else if (rbt_TC1105_Normal_4_OTP.Checked == true)
            //{ Send_buf[3] = 0x40; }
            //else if (rbt_TC1105_HBM_OTP.Checked == true)
            //{ Send_buf[3] = 0x50; }
            //else if (rbt_TC1105_AOD_OTP.Checked == true)
            //{ Send_buf[3] = 0x60; }
            //if (Convert.ToDecimal(IC69091_OTP_Times.Text) == 1)
            //{
            //    //  if(IC69091_OTP_P50.Checked==true)
            //}
            CRCData.CalculateCrc16(Send_buf, out Send_buf[Setting.TempRecord.cmdLen - 2], out Send_buf[Setting.TempRecord.cmdLen - 1]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, Setting.TempRecord.cmdLen);   //
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void btn_DeMura_Load_Data_Click(object sender, EventArgs e)
        {
            PreLoad_Demura_data_Click();
        }

    }
}
